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

Refreshing text in a TextBox at intervals of 1 second

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

EDIT*** 所以我用 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

您在声明变量时设置了变量 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