计时器在Vb.net中不起作用

时间:2017-03-15 17:59:48

标签: vb.net timer textbox

当我运行此代码时,它会在两个单独的文本框中显示日期和时间,但计时器不会运行。我的意思是秒部分没有移动...这是我的代码谢谢

    Private Sub Studentattend_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Text = TimeString
    TextBox2.Text = DateString
    Label4.Text = Now
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Label4.Text = TimeString
    Label4.Text = Now
End Sub

1 个答案:

答案 0 :(得分:2)

Timeline

没有必要将Label4.Text更改两次,只需使用Now.ToString()来格式化时间。 例如,Label4.Text = Now.ToString(" hh:mm:ss")将以hh:mm:ss格式显示当前时间。此外,请确保计时器间隔(以毫秒为单位)是正确的。如果您想更新文本框,则必须在Timer1_Tick中添加代码:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Label4.Text = TimeString
    Label4.Text = Now
End Sub