Reputation: 13
I was playing around trying to learn about using timers in Visual Basic (I'm using Visual Studio 2013 Professional) as I am only starting out. I wrote a short piece of code to open a form (containing a label that read "Welcome" in black text, so I wouldn't show up), which would start a timer that would trigger different sentences to come up. Here is the code:
Public Class Form1
Private TimerTicks As Integer = Nothing
Private userName As String = "Phillip"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.Black
Timer.Interval = 1000
Timer.Enabled = True
TextLabel.ForeColor = Color.Black
TextLabel.BackColor = Color.Black
EventsLine()
End Sub
Private Sub EventsLine()
Do Until TimerTicks = 20
If TimerTicks = 1 Then
TextLabel.ForeColor = Color.White
ElseIf TimerTicks = 3 Then
TextLabel.Text = "Your name is " & userName & ", right?"
ElseIf TimerTicks = 10 Then
TextLabel.Text = "Nice to meet you"
End If
Loop
Me.Hide()
End Sub
Private Sub Timer_Tick() Handles Timer.Tick
TimerTicks = TimerTicks + 1
End Sub
End Class
There is obviously something horrifically wrong with my code as when I run the program, the form doesn't even show up. I don't know if it's loading or just hiding as I tried using 'Me.Show()' in the 'Form1_Load' Sub and it showed, but It just didn't respond or show anything.
This is all for learning purposes, so please feel free to rip my code apart and tell me every mistake I've made, but please be nice about it :)
Upvotes: 1
Views: 1847
Reputation: 1512
Put the 'Private Sub EventsLine() in the timer but don't use the do until, then it works.
If you use If TimerTicks < 20 Then , you get the same.
Private TimerTicks As Integer = Nothing
Private userName As String = "Phillip"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.Black
Timer.Interval = 1000
Timer.Enabled = True
TextLabel.ForeColor = Color.Black
TextLabel.BackColor = Color.Black
End Sub
'Private Sub EventsLine()
' Do Until TimerTicks = 20
' If TimerTicks = 1 Then
' TextLabel.ForeColor = Color.White
' ElseIf TimerTicks = 3 Then
' TextLabel.Text = "Your name is " & userName & ", right?"
' ElseIf TimerTicks = 10 Then
' TextLabel.Text = "Nice to meet you"
' End If
' Loop
' Me.Hide()
'End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
TimerTicks = TimerTicks + 1
If TimerTicks < 20 Then
If TimerTicks = 1 Then
TextLabel.ForeColor = Color.White
ElseIf TimerTicks = 3 Then
TextLabel.Text = "Your name is " & userName & ", right?"
ElseIf TimerTicks = 10 Then
TextLabel.Text = "Nice to meet you"
End If
Else
End If
If TimerTicks = 20 Then
Me.Hide()
End If
End Sub
Upvotes: 1
Reputation: 35270
The basic problem is that you're using a timer that is running on the same thread as the UI thread, and the UI thread is being blocked by the EventsLine
method (which is called from the Load
event). Since the event message loop never reaches the Tick
handler, there's never an opportunity to increment timer ticks.
I would suggest reading up on the differences between timers in .NET. Some of them are better for multi-threaded operations. This is a good resource:
http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
Also, you might also want to learn something about the way that event-driven applications function. http://en.wikipedia.org/wiki/Event_loop
Upvotes: 0