when clicking on the start button in the code below, the form seems to lose focus and I need to click the stop button twice to stop the count. (First click to activate the form, second to click the button) Can someone please explain this behavior or offer a better alternative?
Public Class Form1
Dim testrunning As Boolean
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
testrunning = True
test()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
testrunning = False
End Sub
Private Sub test()
Dim count As Integer
While testrunning = True
count += 1
TextBox1.Text = count.ToString
System.Threading.Thread.Sleep(100)
Application.DoEvents()
End While
End Sub
End Class