调用下面的类,代码没有错误.但是label1的时间只增加1秒,就停在那儿不动了啊!!(label1应该是计时的时间的显示)form1窗体中的代码如下:Timer类的代码在后面using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication36
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
int hour = 0; int minute = 0; int second = 0;
private void timer1_Tick(object sender, EventArgs e)
{
Time time = new Time(hour, minute, second);
time ++;
this.label1.Text = "时间在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
} private void Form1_Load(object sender, EventArgs e)
{
} private void timer2_Tick(object sender, EventArgs e) {
DateTime dtime = DateTime.Now;
int hour = dtime.Hour;
int minute = dtime.Minute;
int second = dtime.Second;
this.label2.Text = "当前时间" + hour + ":" + minute + "" + ":" + second; }
}
}Timer类里面的代码如下:using System;
using System.Collections.Generic;
using System.Text;namespace WindowsApplication36
{
class Time
{ private int hours; //小时
private int minutes; //分钟
private int seconds; //秒钟 public Time()
{
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
} public Time(int hours, int minutes, int seconds)
{
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
} public void SetHours(int hours)
{
this.hours = hours;
} public void SetMinutes(int minutes)
{
this.minutes = minutes;
} public void SetSeconds(int seconds)
{
this.seconds = seconds;
} public int GetHours()
{
return this.hours;
} public int GetMinutes()
{
return this.minutes;
} public int GetSeconds()
{
return this.seconds;
} public static Time operator ++(Time time)
{
time.seconds++;
if (time.seconds >= 60)
{
time.minutes++;
time.seconds = 0;
if (time.minutes >= 60)
{
time.hours++;
time.minutes = 0;
time.seconds = 0;
if (time.hours >=24 )
{
time.hours = 0;
time.minutes = 0;
time.seconds = 0;
}
}
}
return new Time(time.hours,time.minutes,time.seconds);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication36
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
int hour = 0; int minute = 0; int second = 0;
private void timer1_Tick(object sender, EventArgs e)
{
Time time = new Time(hour, minute, second);
time ++;
this.label1.Text = "时间在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
} private void Form1_Load(object sender, EventArgs e)
{
} private void timer2_Tick(object sender, EventArgs e) {
DateTime dtime = DateTime.Now;
int hour = dtime.Hour;
int minute = dtime.Minute;
int second = dtime.Second;
this.label2.Text = "当前时间" + hour + ":" + minute + "" + ":" + second; }
}
}Timer类里面的代码如下:using System;
using System.Collections.Generic;
using System.Text;namespace WindowsApplication36
{
class Time
{ private int hours; //小时
private int minutes; //分钟
private int seconds; //秒钟 public Time()
{
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
} public Time(int hours, int minutes, int seconds)
{
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
} public void SetHours(int hours)
{
this.hours = hours;
} public void SetMinutes(int minutes)
{
this.minutes = minutes;
} public void SetSeconds(int seconds)
{
this.seconds = seconds;
} public int GetHours()
{
return this.hours;
} public int GetMinutes()
{
return this.minutes;
} public int GetSeconds()
{
return this.seconds;
} public static Time operator ++(Time time)
{
time.seconds++;
if (time.seconds >= 60)
{
time.minutes++;
time.seconds = 0;
if (time.minutes >= 60)
{
time.hours++;
time.minutes = 0;
time.seconds = 0;
if (time.hours >=24 )
{
time.hours = 0;
time.minutes = 0;
time.seconds = 0;
}
}
}
return new Time(time.hours,time.minutes,time.seconds);
}
}
}
解决方案 »
- Form_Load()怎么没有被调用
- web,textbox离开
- 启用一个新线程和 使用委托.BeginInvoke 有什么区别吗?
- 用.net2.0激活iphone
- 各位大哥帮一下忙
- 关于在DATASET中修改数据的问题
- 面试题
- 请问使用Remoting时,如何在一个程序里面同时实现服务器端和客户端? 急
- Wpf RichTextBox如何删除最后一行(求助各位大神)
- 为什么c#不能运行???大家帮忙看看
- devexpress的VGridControl控件中EditValueChanged事件的获取被包装的RepositoryItem
- C#备注信息的一些问题。应该是很简单的,但是平常很少注意这个问题。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication36
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int hour = 0; int minute = 0; int second = 0;
Time time =null;
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
time = new Time(hour, minute, second);
}
private void timer1_Tick(object sender, EventArgs e)
{
time ++;
this.label1.Text = "时间在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
} private void Form1_Load(object sender, EventArgs e)
{
} private void timer2_Tick(object sender, EventArgs e) {
DateTime dtime = DateTime.Now;
int hour = dtime.Hour;
int minute = dtime.Minute;
int second = dtime.Second;
this.label2.Text = "当前时间" + hour + ":" + minute + "" + ":" + second; }
}
}
Time time =null;
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
time = new Time(hour, minute, second);
}
修改这段代码后.问题没得到解决还多出一个警告
警告 1 私有字段“WindowsApplication36.Form1.time”已被赋值,但从未使用过它的值 D:\My Documents\Visual Studio 2005\Projects\WindowsApplication36\WindowsApplication36\Form1.cs 23 14 WindowsApplication36
楼上?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace WindowsApplication36
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); }
int hour = 0; int minute = 0; int second = 0;
Time time =new Time();
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
time.SetMinutes(minute );
....等等
你只出现一秒的的原因是 你每次都new 一个新time对象。所以他每次都只走一秒 }
private void timer1_Tick(object sender, EventArgs e)
{
time ++;
this.label1.Text = "时间在流逝:" + time.GetHours() + ":" + time.GetMinutes() + ":" + time.GetSeconds();
} private void Form1_Load(object sender, EventArgs e)
{
} private void timer2_Tick(object sender, EventArgs e) {
DateTime dtime = DateTime.Now;
int hour = dtime.Hour;
int minute = dtime.Minute;
int second = dtime.Second;
this.label2.Text = "当前时间" + hour + ":" + minute + "" + ":" + second; }
}
}
比如在timer tick事件里判断time是不时null的
ericzhangbo1982111
代码是正确的啊...
不好意思了啊