It doesn't work because the
Elapsed event is raised on a different thread[
^] from the UI thread (startup). You cannot touch UI controls from anything other than the UI thread. In this case, you're going to have to Invoke a method on the UI thread to change the text in the TextBox.
Private aTimer As Timer = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
aTimer = New Timer() With {
.Interval = 2000
}
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.AutoReset = True
aTimer.Enabled = True
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
SetTextBoxText("Elapsed")
End Sub
Private Sub SetTextBoxText(message As String)
If Me.InvokeRequired Then
Invoke(Sub()
SetTextBoxText(message)
End Sub)
Else
TextBox1.Text = "Elapsed"
End If
End Sub