6 Replies - 559 Views - Last Post: 15 June 2019 - 04:35 AM Rate Topic: -----

#1 nmjm20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 19

Basic Traffic Light controlled by tab key

Posted 09 June 2019 - 04:15 PM

Hey guys. I’m having a little issue with my hw. I have all of the textboxes properly placed, named and my code seems correct. However, when I press the tab key it doesn’t cycle the colors. The only way the colors change is when I press tab, followed by typing/pressing the space key.

Here is my code:
Public Class frmTL
    Private Sub txtGreen_TextChanged(sender As Object, e As EventArgs) Handles txtGreen.TextChanged
        'Nicholas Mercurio

        txtGreen.BackColor = Color.Green
        txtYellow.BackColor = Color.Gray
        txtRed.BackColor = Color.Gray
    End Sub

    Private Sub txtYellow_TextChanged(sender As Object, e As EventArgs) Handles txtYellow.TextChanged
        txtGreen.BackColor = Color.Gray
        txtYellow.BackColor = Color.Yellow
        txtRed.BackColor = Color.Gray
    End Sub

    Private Sub txtRed_TextChanged(sender As Object, e As EventArgs) Handles txtRed.TextChanged
        txtGreen.BackColor = Color.Gray
        txtYellow.BackColor = Color.Gray
        txtRed.BackColor = Color.Red
    End Sub
End Class
:code:

Any hints you'd be able to give me?

Thanks in advance,
Nick

I should probably add the homework question:

Simulate a traffic light with three small square text boxes placed vertically on a form.

Initially, the bottom text box is solid green and the other text boxes are dark gray. When the Tab key is pressed, the middle text box turns yellow and the bottom text box turns dark gray. The next time Tab is pressed, the top text box turns red and the middle text box turns dark gray. Subsequent pressing of the Tab key cycles through the three colors. Hint: First place the bottom text box on the form, then the middle text box and finally the top text box.

This post has been edited by modi123_1: 10 June 2019 - 07:17 AM
Reason for edit:: In the future please use the [code] button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Basic Traffic Light controlled by tab key

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,113
  • Joined: 12-January 10

Re: Basic Traffic Light controlled by tab key

Posted 10 June 2019 - 07:20 AM

not sure why you are using text change if youre not adding text

also that isnt all the code-- where is the rest
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,480
  • Joined: 12-June 08

Re: Basic Traffic Light controlled by tab key

Posted 10 June 2019 - 07:20 AM

I would think you would want to investigate a different event.

If you are looking at your designer, right click on a textbox and click 'properties'. In that properties window click the little lightening bolt. That should give you a list of available events for that control.

Scrolling through I would think a 'focus enter' would do well. You can double click on the event and VS sets it up for you.
Was This Post Helpful? 0
  • +
  • -

#4 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 431
  • View blog
  • Posts: 1,361
  • Joined: 02-November 11

Re: Basic Traffic Light controlled by tab key

Posted 11 June 2019 - 06:05 AM

Focus Enter would be a good way to do it. Another way would be the Form.KeyDown or KeyUp event, then check for TAB as pressed then just cycle them on an if-then-else structure

Pseudo for KeyDown/Up would be:

If (Is press key TAB) Then
    If (Is Green Lit up) Then
        Light up Red
    Else If (Is Red lit up) Then
        Light up Yellow
    Else If (Is Yellow lit up) Then
        Light up Green
    End If
End IF

It really depends on what the focus of the homework is, as in what the lesson which prompted the homework was about.
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 124
  • View blog
  • Posts: 684
  • Joined: 01-October 08

Re: Basic Traffic Light controlled by tab key

Posted 12 June 2019 - 09:07 AM

For the heck of it I made this simple Traffic Light simulator class that uses textboxes

Public Class TrafficLights
    Private redLT As TextBox
    Private yelLT As TextBox
    Private grnLT As TextBox

    Public Sub New(redLT As TextBox,
                   yelLT As TextBox,
                   grnLT As TextBox)

        Me.redLT = redLT
        Me.yelLT = yelLT
        Me.grnLT = grnLT
        Me.redLT.BackColor = Color.Black
        Me.yelLT.BackColor = Color.Black
        Me.grnLT.BackColor = Color.Black
        Me.StartSim()
    End Sub

    Private LtsTask As Task
    Private LtsStop As New Threading.ManualResetEvent(False)

    Public Sub StartSim()
        If Me.LtsTask Is Nothing Then
            Me.LtsStop.Reset()
            Me.LtsTask = Task.Run(Sub() DoLights())
        End If
    End Sub

    Public Sub StopSim()
        Me.LtsStop.Set()
        If Me.LtsTask IsNot Nothing Then
            Me.LtsTask.Wait()
            Me.LtsTask = Nothing
        End If
    End Sub

    Private Sub DoLights()
        Dim redON As Boolean = True
        Dim yelON As Boolean = False
        Dim grnON As Boolean = False
        Dim redTM As TimeSpan = TimeSpan.FromSeconds(7)
        Dim yelTM As TimeSpan = TimeSpan.FromSeconds(3)
        Dim grnTM As TimeSpan = TimeSpan.FromSeconds(11)

        Dim stpw As New Stopwatch
        stpw.Restart()

        Do
            Select Case True
                Case redON
                    If stpw.Elapsed >= redTM Then
                        redON = False
                        grnON = True
                        stpw.Restart()
                    End If
                Case grnON
                    If stpw.Elapsed >= grnTM Then
                        grnON = False
                        yelON = True
                        stpw.Restart()
                    End If
                Case yelON
                    If stpw.Elapsed >= yelTM Then
                        yelON = False
                        redON = True
                        stpw.Restart()
                    End If
            End Select

            'use .BeginInvoke!
            Me.redLT.BeginInvoke(Sub()
                                     Select Case True
                                         Case redON
                                             Me.redLT.BackColor = Color.Red
                                             Me.yelLT.BackColor = Color.Black
                                             Me.grnLT.BackColor = Color.Black

                                         Case yelON
                                             Me.redLT.BackColor = Color.Black
                                             Me.yelLT.BackColor = Color.Yellow
                                             Me.grnLT.BackColor = Color.Black

                                         Case grnON
                                             Me.redLT.BackColor = Color.Black
                                             Me.yelLT.BackColor = Color.Black
                                             Me.grnLT.BackColor = Color.Green

                                     End Select
                                 End Sub)

        Loop While Not Me.LtsStop.WaitOne(1000)
        'Stopped
        Me.redLT.BeginInvoke(Sub()
                                 Me.redLT.BackColor = Color.Black
                                 Me.yelLT.BackColor = Color.Black
                                 Me.grnLT.BackColor = Color.Black
                             End Sub)
    End Sub
End Class


To Test it

    Dim TrafLt As TrafficLights = Nothing
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If TrafLt Is Nothing Then
            TrafLt = New TrafficLights(txtRed, txtYel, txtGrn)
        End If
        TrafLt.StartSim()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        TrafLt.StopSim()
    End Sub


Was This Post Helpful? 0
  • +
  • -

#6 nmjm20   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-April 19

Re: Basic Traffic Light controlled by tab key

Posted 14 June 2019 - 08:54 PM

I ended up figuring it out. Thanks guys...I added "enter" and "leave" events.
Was This Post Helpful? 0
  • +
  • -

#7 TechnoBear   User is offline

  • Lady A
  • member icon

Reputation: 431
  • View blog
  • Posts: 1,361
  • Joined: 02-November 11

Re: Basic Traffic Light controlled by tab key

Posted 15 June 2019 - 04:35 AM

That's what I enjoy most about programming. There is always more than one way to skin a cat.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1