C#获取时间问题.

来源:百度知道 编辑:UC知道 时间:2024/05/13 19:25:06
各位,我做一个窗体,用一个lable来显示时间.
lable1.text="时间"+System.DateTime.Now.ToLocalTime();
这个可以显示.但是有个问题,显示出来的时间就不会改变了..比如3:10就一直是这个数字.只有关闭窗体重新打开窗体后才又变成了现在的时间..请问一下大家用个什么方法来实现在label里显示的时间是变化的.

使用计时器(Timer)

private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "时间:" + DateTime.Now.ToString();
Timer timer1 = new Timer();
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "时间:" + DateTime.Now.ToString();
}

你注意看看有没有混用“=”和“==”