Traffic Light

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 10658 Views - Last Post: 23 February 2012 - 12:04 PM Rate Topic: -----

#1 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Traffic Light

Posted 08 September 2010 - 05:28 AM

Problem:

Simulate a traffic light with a control array consisting of three small square picture boxes placed vertically on a form. initially the bottom picture box is solid green and the other picture boxes are white. when the tab key is pressed, the middle picture box turns yellow and the bottom picture box turns white. the next time tab is pressed, the top picture box turns red and the middle picture box turns white. subsequent pressing of the tab key cycles through the three colors. Hint: First, place the bottom picture box on the form then the middle picture box and finally the top picture box.

Private Sub Form_KeyPress(vbkeytab As Integer)
If tabindex = 1 Then
Picture2.BackColor = vbYellow
Picture1.BackColor = vbWhite
ElseIf tabindex = 2 Then
Picture3.BackColor = vbRed
Picture2.BackColor = vbWhite
Picture1.BackColor = vbWhite
End If
End Sub



please help me... I don't know why its not working.... :"( what should i use to make it work? a vbkeytab or tabindex? is tabindex really needed? do i need to make it a control array? how??? PLEASE HELP... :(

Is This A Good Question/Topic? 0
  • +

Replies To: Traffic Light

#2 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Traffic Light

Posted 08 September 2010 - 06:04 AM

i made another, but still its not working... :(

Private Sub Form_KeyPress(tabindex As Integer)

If vbkeytab(tabindex) = 1 Then
    Picture2.BackColor = vbYellow
    Picture1.BackColor = vbWhite
ElseIf vbkeytab(tabindex) = 2 Then
    Picture3.BackColor = vbRed
    Picture2.BackColor = vbWhite
    Picture1.BackColor = vbWhite
End If
End Sub



Was This Post Helpful? 0
  • +
  • -

#3 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 819
  • Joined: 16-September 09

Re: Traffic Light

Posted 08 September 2010 - 09:25 AM

I don't know if I follow completely and I don't know the platform but there are a couple of options

1. Instead of pictures use shapes...if they are actually pictures
2. Instead of "vbColor" try using the numbers.
3. With the keypress staying as the function, what's the key that activates it? Try changing to a command button for testing purposes then once you've established that the code works, change back to key press.


What platform are you writing this in? Whats the key you're pressing? What happens now when you press the key? What works and what doesn't? Answer these and hopefully I can elaborate a bit more.

I know it's not much, but I hope this helps!
Was This Post Helpful? 1
  • +
  • -

#4 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Traffic Light

Posted 08 September 2010 - 10:47 PM

Yes, we are instructed to use a picture box. I already saw a sample of a traffic light, but it uses timer, but for us we are not allowed to use it. the keypress that needs to be activated is the Tab key. this is a visual basic program. everytime i press the Tab key in the keyboard, still nothing happens. :(
Was This Post Helpful? 0
  • +
  • -

#5 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Traffic Light

Posted 09 September 2010 - 07:11 AM

do i still need to use a vbkeytab? how to access the tab index? is is still needed?
Was This Post Helpful? 0
  • +
  • -

#6 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 819
  • Joined: 16-September 09

Re: Traffic Light

Posted 09 September 2010 - 08:29 AM

I've been thinking about this....what about changing the reference to the tab key.


Sub test(TabKeyASCii As Integer)

If TabKeyASCii = 13 Then
    If Picture2.BackColor = vbYellow Then
        Picture2.BackColor = vbYellow
        Picture1.BackColor = vbWhite
        Exit Sub
    ElseIf Picture2.BackColor = vbWhite Then
        Picture3.BackColor = vbRed
        Picture2.BackColor = vbWhite
        Picture1.BackColor = vbWhite
        Exit Sub
    End If 'Pic2.BackColor
End If 'TabKeyASCii

End Sub



no promises on this but I hope it helps or at least give you a different view that may help!

This post has been edited by guyfromri: 09 September 2010 - 08:30 AM

Was This Post Helpful? 1
  • +
  • -

#7 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Traffic Light

Posted 09 September 2010 - 09:05 AM

still, its not working... :( i already change some codes... :(

i also tried to use a control array for the picture boxes, but i can't access it. :( what's the use of tab index?
Was This Post Helpful? 0
  • +
  • -

#8 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Traffic Light

Posted 09 September 2010 - 10:17 AM

what is the error you got. why is not working what is the code that you have used. tabindex is custom made variable in original function Form_KeyPress() looks like this:
Private Sub Form_KeyPress(KeyAscii As Integer)
    Dim x As Integer
    x = 1
End Sub



this code make variable x and make i equal to 1. As you can see the original variable is KeyAscii as integer, but you can change it to what ever you want. now about what this variable do is pass the ascii code of the key that is pressed in the form. for example key return ascii code is 13(as in the example of guyfromri). you can find more ascii codes here Ascii Table

as you can see from the link the ascii code in decimal for the tab key is 9. so if you want to use the tab key you have to check if the variable that return the key code is equal to 9(in original variable KeyAscii). here is an example:
Private Sub Form_KeyPress(KeyAscii As Integer)
'enter the if statement if the tab key is pressed'
'and message box show up'
    If KeyAscii = 9 Then
        MsgBox "Gratz you click the Tab key."
    End If
End Sub



we will be glad to help you with any other questions you may have :)
Was This Post Helpful? 1
  • +
  • -

#9 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,796
  • Joined: 26-March 09

Re: Traffic Light

Posted 09 September 2010 - 10:20 AM

Hi princess,

The clue is in the hint!

It tells you to place the bottom picturebox first.

Add them as it states creating a control array of picture1.

If you now click on one of the picture boxes, then look at the properties, you will see the TabIndex, goes from 0 to 2 (bottom to top). The TabIndex determines which controls become active and in which order, so in this case, bottom to top.

Now what you need to do, is use the GotFocus method of the picturebox control. In this method, it will use the Index as the input. You can then use the index to set the colours accordingly.

Hope that makes sense.

Give it a go......(please don't press the - button this time! :) ).
Was This Post Helpful? 1
  • +
  • -

#10 parbipin  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 95
  • Joined: 23-August 10

Re: Traffic Light

Posted 09 September 2010 - 10:34 AM

To Add to what NoBrain suggested:

Also don't forget to set the Key Preview property of the Form to be set to True
Was This Post Helpful? 2
  • +
  • -

#11 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Traffic Light

Posted 09 September 2010 - 10:36 AM

totally i always forget about it. :D

This post has been edited by NoBrain: 09 September 2010 - 10:36 AM

Was This Post Helpful? 1
  • +
  • -

#12 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Traffic Light

Posted 11 September 2010 - 06:21 AM

Please help me... still its not working... :((

this is the new code... :((

Private Sub Form_KeyPress(KeyAscii As Integer)
  'enter the if statement if the tab key is pressed'
   'and message box show up'
       Select Case KeyAscii
        Case 9
           If Picture1(1).GotFocus = True Then
           Picture1(1).BackColor = vbYellow
           Picture1(0).BackColor = vbWhite
           ElseIf Picture1(2).GotFocus = True Then
           Picture1(2).BackColor = vbRed
           Picture1(1).BackColor = vbWhite
           Picture1(0).BackColor = vbWhite
           End If
        End Select
   End Sub



To: parbipin
what's with the keypreview? may i know its function? thank you! :)

To: NoBrain
the sample code you've given is not working. there's no msgbox appearing... :( please help... :((


THANK YOU! :(
Was This Post Helpful? 1
  • +
  • -

#13 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Traffic Light

Posted 11 September 2010 - 10:18 AM

do you set your form keypreview property to true?
Private Sub Form_Load()
Me.KeyPreview = True
End Sub


This post has been edited by NoBrain: 11 September 2010 - 10:19 AM

Was This Post Helpful? 1
  • +
  • -

#14 Shayan.To10  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 7
  • Joined: 10-September 10

Re: Traffic Light

Posted 12 September 2010 - 12:17 AM

Hi.
You can use this.
But remember to:
1- First place the bottom PictureBox then the middle one and at last the top PictureBox.
2- Do not make an array with PictureBoxes.(If you copy a PictureBox and then paste it answer No to the question.)

Dim l As Integer

Private Sub Form_Load()
    Me.KeyPreview = True
    l = 1
    Picture1.BackColor = vbGreen
    Picture2.BackColor = vbWhite
    Picture3.BackColor = vbWhite
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
        Picture1.BackColor = vbWhite
        Picture2.BackColor = vbWhite
        Picture3.BackColor = vbWhite
        l = l + 1
        If l > 3 Then l = 1
        Select Case l
            Case 1
                Picture1.BackColor = vbGreen
            Case 2
                Picture2.BackColor = vbYellow
            Case 3
                Picture3.BackColor = vbRed
        End Select
    End If
End Sub


If that code doesn't work try this one (But still remember those!):

Dim l As Integer

Private Sub Form_Load()
    Me.KeyPreview = True
    l = 1
    Picture1.BackColor = vbGreen
    Picture2.BackColor = vbWhite
    Picture3.BackColor = vbWhite
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
        Picture1.BackColor = vbWhite
        Picture2.BackColor = vbWhite
        Picture3.BackColor = vbWhite
        l = l + 1
        If l > 3 Then l = 1
        Select Case l
            Case 1
                Picture1.BackColor = vbGreen
            Case 2
                Picture2.BackColor = vbYellow
            Case 3
                Picture3.BackColor = vbRed
        End Select
    End If
End Sub

Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
        Picture1.BackColor = vbWhite
        Picture2.BackColor = vbWhite
        Picture3.BackColor = vbWhite
        l = l + 1
        If l > 3 Then l = 1
        Select Case l
            Case 1
                Picture1.BackColor = vbGreen
            Case 2
                Picture2.BackColor = vbYellow
            Case 3
                Picture3.BackColor = vbRed
        End Select
    End If
End Sub

Private Sub Picture2_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
        Picture1.BackColor = vbWhite
        Picture2.BackColor = vbWhite
        Picture3.BackColor = vbWhite
        l = l + 1
        If l > 3 Then l = 1
        Select Case l
            Case 1
                Picture1.BackColor = vbGreen
            Case 2
                Picture2.BackColor = vbYellow
            Case 3
                Picture3.BackColor = vbRed
        End Select
    End If
End Sub

Private Sub Picture3_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
        Picture1.BackColor = vbWhite
        Picture2.BackColor = vbWhite
        Picture3.BackColor = vbWhite
        l = l + 1
        If l > 3 Then l = 1
        Select Case l
            Case 1
                Picture1.BackColor = vbGreen
            Case 2
                Picture2.BackColor = vbYellow
            Case 3
                Picture3.BackColor = vbRed
        End Select
    End If
End Sub

Was This Post Helpful? 2
  • +
  • -

#15 princess18  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 07-December 09

Re: Traffic Light

Posted 12 September 2010 - 07:57 AM

To: NoBrain
Yes, I've set the form's property to TRUE... But still, nothing happens...

To: Shayan.To10
THANK YOU VERY MUCH FOR THE EFFORT!!! THANK YOU!!! WELL APPRECIATED...:') but still, nothing happens when i press the TAB key... :(( the picture boxes has colors when the form is loaded but it doesn't change as you click the tab key... :( why is that the picture boxes should not be arrayed? the instruction tells that it should be arrayed. is that impossible?...

does the TABINDEX has something to do with this?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2