以1秒钟的间隔刷新TextBox中的文本

时间:2019-04-22 20:14:11

标签: vb.net visual-studio-2010 timer textbox

我正在尝试在文本框中显示本地时间,但需要刷新...我使用计时器希望刷新时间,但它似乎没有重印我的文本。如果您能花时间帮助我,那就太好了!

编辑***因此,我尝试使用TextBox.AppendText()来查看如果它不断重印会发生什么,我注意到日期和时间根本没有更新。我需要刷新表格吗?

Public Class Form1

    Dim t As String = My.Computer.Clock.LocalTime
    Dim m As String = t & vbCrLf & " - Time Left - "
    Private Timer As System.Windows.Forms.Timer
    Private TimerCounter As Integer = 0
    Dim TempText As String = m

    Protected Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
        TextBox.TextAlign = HorizontalAlignment.Center
        TimerCounter += 1
        TextBox.Text = t
    End Sub

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Shown 'this goes with the line just above
        Timer = New Windows.Forms.Timer With {.Interval = 1000}
        AddHandler Timer.Tick, AddressOf TimerTick
        Timer.Start()
    End Sub

End Class

我的预期结果是,如果本地时间在每次计时器计时时在textbox1中更新。

1 个答案:

答案 0 :(得分:2)

您在声明变量时设置了变量 t ,但从未对其进行更新。因此它始终包含相同的值。
实际上,您甚至不需要该变量。您可以仅将TextBox.Text设置为My.Computer.Clock.LocalTime

Protected Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)

    ' You can set this property just one time when you define your TextBox
    ' TextBox.TextAlign = HorizontalAlignment.Center
    TimerCounter += 1
    TextBox.Text = My.Computer.Clock.LocalTime
End Sub