我想每秒刷新TextBox数字时钟(Windows App)

时间:2013-02-17 17:03:22

标签: c# xml windows-phone-7 window

C#代码

TextTime.Text = DateTime.Now.ToString();

想要每秒刷新此文本框

或显示数字时钟任何想法

2 个答案:

答案 0 :(得分:5)

您可以使用DispatcherTimer

var timer = new DispatcherTimer
{
    Interval = TimeSpan.FromSeconds(1.0)
};
timer.Tick += (o, e) =>
{
    TextTime.Text = DateTime.Now.ToString();
};
timer.Start();

答案 1 :(得分:3)

简单的方法是为您的应用添加一个计时器,并按照所示进行操作:

表单加载:

private void Form1_Load(object sender, EventArgs e) {
   txtdate.Text = DateTime.Now.ToString(("dddd" + ("," + "MM-dd-yyyy")));
   Timer1.Interval = 1000;
   Timer1.Enabled = true;
}

计时器勾号:

private void Timer1_Tick(object sender, EventArgs e) {
   txtTime.Text = DateTime.Now.ToString("HH:mm:ss");
}