traffic..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaja99
    New Member
    • Jan 2007
    • 1

    traffic..

    hello to all..

    PLEASE HELP ME!!


    i wanted to make a traffic light in vb using if then else..
    3 circles (red, yellow & green) and a timer

    i don't know how to make them appear one by one.. it's okay if it blinks.. this is the sequence..(red, yellow,green.re d,yellow,green. ............... )
    i tried to make one but it came up with this kind of sequence..(red, yellow,red,gree n)

    PLEASE HELP ME!!

    [CODE=vb]
    Private Sub Timer1_Timer()
    If shape1.Visible = False Then
    shape1.Visible = True
    Else
    shape1.Visible = False

    If shape2.Visible = False Then
    shape2.Visible = True
    Else
    shape2.Visible = False

    If shape2.Visible = False Then
    shape2.Visible = True
    Else
    shape2.Visible = False
    end if
    end if
    end if
    end sub
    [/CODE]
    Last edited by debasisdas; Nov 7 '07, 05:46 AM. Reason: Formatted using code tags.
  • dxpert
    New Member
    • Jan 2007
    • 3

    #2
    Hi there, here's the code.

    I created 3 textboxes on my form, named them all Text1 and made them into a control array... so each one of them had the Index property set to 0,1,2

    This should be what you're looking for.
    Good Luck!

    [CODE=vb]
    Option Explicit

    Private mlVisibleShape As Long

    Private Sub Form_Load()
    mlVisibleShape = -1
    Timer1_Timer
    End Sub

    Private Sub Timer1_Timer()

    mlVisibleShape = mlVisibleShape + 1
    If mlVisibleShape > (Me.Text1.Count - 1) Then
    mlVisibleShape = 0
    End If

    SwitchControl
    End Sub

    Private Sub SwitchControl()
    Dim lCtrls As Long

    For lCtrls = 0 To (Me.Text1.Count - 1)
    Me.Text1(lCtrls ).Visible = (lCtrls = mlVisibleShape)
    Next
    End Sub
    [/CODE]
    Last edited by debasisdas; Nov 7 '07, 05:51 AM. Reason: Formatted using code tags.

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Good answer.
      For those of us not familiar with this shorthand approach:
      Code:
      Private Sub SwitchControl()
         Dim lCtrls As Long
         
         For lCtrls = 0 To (Me.Text1.Count - 1)
            If lCtrls = mlVisibleShape Then
               Me.Text1(lCtrls).Visible = True
            Else
               Me.Text1(lCtrls).Visible = False
            End If
         Next
      End Sub

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        If you want a simple modification to get your original code to work, try this...
        Code:
          If Shape1.Visible Then
            Shape1.Visible = False
            Shape2.Visible = True
          ElseIf Shape2.Visible Then
            Shape2.Visible = False
            Shape3.Visible = True
          Else
            Shape3.Visible = False
            Shape1.Visible = True
          End If
        The ElseIf statement is a great space-saver. :)

        Comment

        • utkarshmittal
          New Member
          • Nov 2007
          • 1

          #5
          heres the program (traffic Lights) with a easy coding
          [code=vb]
          Private Sub Form_Load()
          Me.WindowState = 2
          shpred.Visible = True
          shpgreen.Visibl e = False
          shpyellow.Visib le = False
          lblstop.Visible = True
          lblready.Visibl e = False
          lblgo.Visible = False
          Timer1.Enabled = True
          Timer2.Enabled = True
          Timer3.Enabled = True


          End Sub

          Private Sub Timer1_Timer()
          shpred.Visible = True
          lblstop.Visible = True
          shpyellow.Visib le = False
          lblready.Visibl e = False
          shpgreen.Visibl e = False
          lblgo.Visible = False


          Timer2.Enabled = True
          Timer3.Enabled = False




          End Sub

          Private Sub Timer2_Timer()
          shpyellow.Visib le = True
          lblready.Visibl e = True
          shpgreen.Visibl e = False
          lblgo.Visible = False
          shpred.Visible = False
          lblstop.Visible = False

          Timer3.Enabled = True

          Timer1.Enabled = False


          End Sub

          Private Sub Timer3_Timer()
          shpgreen.Visibl e = True
          lblgo.Visible = True
          shpred.Visible = False
          lblstop.Visible = False
          shpyellow.Visib le = False
          lblready.Visibl e = False

          Timer1.Enabled = True
          Timer2.Enabled = False

          End Sub[/code]
          Last edited by debasisdas; Nov 7 '07, 05:52 AM. Reason: Added code=vb tags

          Comment

          Working...