Membuat Simulasi Traffic Light dengan 3 buah Timer di Visual Basic .NET
Selasa, 10 Mei 2016
Edit
ANDA bisa membuat simulasi traffic light dengan 3 buah timer di vb .net
Lihat gambar di bawah ini:
Kode lengkapnya di bawah ini:
Public Class Form1
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Timer1.Interval = 1000
Timer2.Interval = 1000
Timer3.Interval = 1000
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape1.FillColor = Color.White
OvalShape2.FillStyle = PowerPacks.FillStyle.Solid
OvalShape2.FillColor = Color.White
OvalShape3.FillStyle = PowerPacks.FillStyle.Solid
OvalShape3.FillColor = Color.White
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static i As Integer
i = i + 1
Label1.Text = i
If i = 1 Then
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape1.FillColor = Color.Red
End If
If i = 2 Then
OvalShape1.FillColor = Color.White
i = 0
Timer2.Start()
Timer1.Stop()
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Static i As Integer
i = i + 1
Label2.Text = i
If i = 1 Then
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape2.FillColor = Color.Yellow
End If
If i = 2 Then
OvalShape2.FillColor = Color.White
i = 0
Timer3.Start()
Timer2.Stop()
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Static i As Integer
i = i + 1
Label3.Text = i
If i = 1 Then
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape3.FillColor = Color.YellowGreen
End If
If i = 2 Then
OvalShape3.FillColor = Color.White
i = 0
Timer3.Stop()
Timer1.Start()
End If
End Sub
End Class
Lihat gambar di bawah ini:
Untuk membuar form seperti di atas ANDA meletakkan
3 buah Ovalshape
1 buah RectangleShape
3 buah label
3 buah timer
1 buah buttonPublic Class Form1
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Timer1.Interval = 1000
Timer2.Interval = 1000
Timer3.Interval = 1000
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape1.FillColor = Color.White
OvalShape2.FillStyle = PowerPacks.FillStyle.Solid
OvalShape2.FillColor = Color.White
OvalShape3.FillStyle = PowerPacks.FillStyle.Solid
OvalShape3.FillColor = Color.White
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static i As Integer
i = i + 1
Label1.Text = i
If i = 1 Then
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape1.FillColor = Color.Red
End If
If i = 2 Then
OvalShape1.FillColor = Color.White
i = 0
Timer2.Start()
Timer1.Stop()
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Static i As Integer
i = i + 1
Label2.Text = i
If i = 1 Then
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape2.FillColor = Color.Yellow
End If
If i = 2 Then
OvalShape2.FillColor = Color.White
i = 0
Timer3.Start()
Timer2.Stop()
End If
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
Static i As Integer
i = i + 1
Label3.Text = i
If i = 1 Then
OvalShape1.FillStyle = PowerPacks.FillStyle.Solid
OvalShape3.FillColor = Color.YellowGreen
End If
If i = 2 Then
OvalShape3.FillColor = Color.White
i = 0
Timer3.Stop()
Timer1.Start()
End If
End Sub
End Class