Wednesday, 11 May 2016

Creating Traffic Light Using VB

                         Creating Traffic Light Using VB                            


First create the above design of your traffic light in the form designer and then copy the below code into the code window of the vb.

Note: 

Fill the circles or ovals made for the traffic lights with the red,yellow and green color before you start coding.
Also, add the three timers from the toolbox.
AS, this is VB 2010 the timers are not visible but if you are using VB 6 the timers will be visible at the side of each light.
So add 3 timers at the side of each coloured circle or oval.

Code:


 Private Sub OvalShape2_Click(sender As System.Object, e As System.EventArgs) Handles OvalShape2.Click

    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        OvalShape1.Visible = True
        OvalShape2.Visible = False
        OvalShape3.Visible = False
        Timer1.Enabled = False
        Timer2.Enabled = True
    End Sub

    Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick
        OvalShape1.Visible = False
        OvalShape2.Visible = True
        OvalShape3.Visible = False
        Timer2.Enabled = False
        Timer3.Enabled = True
    End Sub

    Private Sub Timer3_Tick(sender As System.Object, e As System.EventArgs) Handles Timer3.Tick
        OvalShape1.Visible = False
        OvalShape2.Visible = False
        OvalShape3.Visible = True
        Timer3.Enabled = False
        Timer1.Enabled = True
    End Sub
    Friend WithEvents RectangleShape1 As Microsoft.VisualBasic.PowerPacks.RectangleShape
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Button6 As System.Windows.Forms.Button

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        Timer1.Enabled = True
        OvalShape1.Visible = True
        OvalShape2.Visible = False
        OvalShape3.Visible = False
    End Sub
    Friend WithEvents Button7 As System.Windows.Forms.Button

    Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        Timer1.Enabled = False
        Timer2.Enabled = False
        Timer3.Enabled = False
        OvalShape1.Visible = False
        OvalShape2.Visible = False
        OvalShape3.Visible = False
    End Sub
End Class

Code for VB 6 Users :


Private Sub Form_Load()


Timer1.Enabled = True


Shape1.Visible = True


Shape2.Visible = False


Shape3.Visible = False


 


End Sub


Private Sub Timer1_Timer()


If Shape1.Visible Then


 


Shape2.Visible = True


Shape1.Visible = False


Shape3.Visible = False


 


ElseIf Shape2.Visible Then


 


Shape3.Visible = True


Shape2.Visible = False


Shape1.Visible = False


 


Else


 


Shape1.Visible = True


Shape2.Visible = False


Shape3.Visible = False


 


End If


 


End Sub

No comments:

Post a Comment