In this instructable I will show you how to make a simple timer in Visual Basic 2010
It has : 4 Buttons
8 Labels
3 Numeric Up Down
1 Timer
It has : 4 Buttons
8 Labels
3 Numeric Up Down
1 Timer
Step 1: New Project
First Open Visual Basic and make a new project and select Windows Forms Application
Step 3: Label the Labels
Label the labels like this or code will not work and you would have to change some of it
label 1 = 00
label 2 = :
label 3 = 00
label 4 = :
label 5 = 00
label 6 = Hours
label 7 = Minutes
label 8 = Seconds
You Should also try to increase font to see the labels better
label 1 = 00
label 2 = :
label 3 = 00
label 4 = :
label 5 = 00
label 6 = Hours
label 7 = Minutes
label 8 = Seconds
You Should also try to increase font to see the labels better
Step 4: Label the Buttons
Label the buttons like this :
Button1 = Set
Button2 = Start
Button3 = Reset
Button4 = Stop
Button1 = Set
Button2 = Start
Button3 = Reset
Button4 = Stop
Step 5: Set timer and NumericUpDown
Now For The Timer and the NumericUpDown Set The Maximum and Minimum Value like this :
NumericUpDown1 = Maximum 24, Minimum 0
NumericUpDown2 = Maximum 59, Minimum 0
NumericUpDown3 = Maximum 59, Minimum 0
Timer Interval = 1000
NumericUpDown1 = Maximum 24, Minimum 0
NumericUpDown2 = Maximum 59, Minimum 0
NumericUpDown3 = Maximum 59, Minimum 0
Timer Interval = 1000
Step 6: The Code
Now For The Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Label1.Text = NumericUpDown1.Text
Label3.Text = NumericUpDown2.Text
Label5.Text = NumericUpDown3.Text
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Label1.Text = "00"
Label3.Text = "00"
Label5.Text = "00"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Timer1.Enabled = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label5.Text = 0 Then
Label5.Text = 59
Label3.Text = Label3.Text - 1
ElseIf Label5.Text > 0 Then
Label5.Text = Label5.Text - 1
End If
If Label3.Text = 0 Then
Label1.Text = Label1.Text - 1
Label3.Text = 59
End If
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Label1.Text = NumericUpDown1.Text
Label3.Text = NumericUpDown2.Text
Label5.Text = NumericUpDown3.Text
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Label1.Text = "00"
Label3.Text = "00"
Label5.Text = "00"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Timer1.Enabled = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label5.Text = 0 Then
Label5.Text = 59
Label3.Text = Label3.Text - 1
ElseIf Label5.Text > 0 Then
Label5.Text = Label5.Text - 1
End If
If Label3.Text = 0 Then
Label1.Text = Label1.Text - 1
Label3.Text = 59
End If
End Sub
End Class
Step 7: Extra
Now You can leave it like that or you can make it do something when its done by adding this code in Timer1 :
Shutdown pc
If Label1.Text = 0 And Label3.Text = 0 And Label5.Text = 0 Then
Timer1.Enabled = False
Shell("shutdown -s")
End If
Message Box
If Label1.Text = 0 And Label3.Text = 0 And Label5.Text = 0 Then
Timer1.Enabled = False
MsgBox("Time Up!", MsgBoxStyle.Critical, "Time Up Time Reached 0")
End If
Shutdown pc
If Label1.Text = 0 And Label3.Text = 0 And Label5.Text = 0 Then
Timer1.Enabled = False
Shell("shutdown -s")
End If
Message Box
If Label1.Text = 0 And Label3.Text = 0 And Label5.Text = 0 Then
Timer1.Enabled = False
MsgBox("Time Up!", MsgBoxStyle.Critical, "Time Up Time Reached 0")
End If
How do you set the timer interval for the NumericUpDown controls?
ReplyDeletenever mind, I realized I was supposed to set the interval with the timer control lol
DeleteThe timer code is honestly pretty bad, here's a better way (could look nicer but it's wayyyy better:
ReplyDeletePrivate Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Label5.Text = 0 Then
Label5.Text = 59
Label3.Text = Label3.Text - 1
ElseIf Label5.Text > 0 Then
Label5.Text = Label5.Text - 1
End If
If Label3.Text < 0 And Label1.Text > 0 Then
Label1.Text = Label1.Text - 1
Label3.Text = 59
ElseIf Label3.Text < 0 Then
Label3.Text = "00"
End If
If Label1.Text < 0 Then
Label1.Text = 0
End If
If Label1.Text = 0 And Label3.Text = 0 And Label5.Text = 0 Then
Timer1.Enabled = False
MsgBox("Time Up!", MsgBoxStyle.Critical, "Time Up Time Reached 0")
End If
End Sub