日期:2014-05-17  浏览次数:20746 次

关于显示时间

private void InitializeTimer()
        {
            // Call this procedure when the application starts.
            // Set to 1 second.
            timer1.Interval = 1000;
            timer1.Tick += new EventHandler(timer1_Tick);

            // Enable timer.
            timer1.Enabled = true;
        }
 private void timer1_Tick(object sender, EventArgs e)
        {
            label4.Text = System.DateTime.Now.ToString("H:M");
        }
按我的设想应该是label4显示当前时间。
结果label4根本不变。
   private void label4_Click(object sender, EventArgs e)
        {
            label4.Text = System.DateTime.Now.ToString("H:M");
        }
我又写了这样一句,结果一点它就显示了。
求教哪里出问题了
------最佳解决方案--------------------
1:楼主可以在Form窗体初始化的时候或者Form的构造函数里面调用InitializeTimer()方法
2:楼主完全也可以手动直接设置以下属性,VS会自动处理(Form2.Designer.cs)

            this.timer1.Enabled = true;
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

------其他解决方案--------------------
timer1.Start() ???
------其他解决方案--------------------
引用:
timer1.Start() ???

timer1.Enabled = true;就实现这个功能了吧
------其他解决方案--------------------
你在哪里调用InitializeTimer()了?
------其他解决方案--------------------
System.DateTime.Now.ToString("HH:mm:ss");
获取时间的问题你试试这个获取时间
------其他解决方案--------------------
你在load时间中没有调用 InitializeTimer 这个方法设置time的属性,所以触发不了time的事件
------其他解决方案--------------------
你在窗体的加载事件里面调用下你写的那个时间控件,可以简便一点:label4.Text = DateTime.Now()
在时间Tick事件里面每次加一秒就行了
------其他解决方案--------------------
没有在正确的位置调用这个Timer_Tick的事件吧
------其他解决方案--------------------
表示看不懂····白学这么多年计算机了
------其他解决方案--------------------
虽然是路过,但是也从中学到了点东西。
------其他解决方案--------------------
到Load加上timer1.Start()试试
引用:
private void InitializeTimer()
        {
            // Call this procedure when the application starts.