So I have a question about timers and need some help...
I'm still learning VB.NET programming, so I'm a beginner here...but so far, with timers and all, I've been able to do them well. My last assignment dealing with timers went very smooth...one of the things was to create timers to change the background color of the form and loop through several colors every 5 seconds, than have a button to change the timer intervals to 2 seconds. I did everything, and it worked.
Than came the next assignment. Traffic Lights. I have to have 3 timers, one for the red light, one for yellow, and one for green. I used pictures are my traffic lights. Here is how the timers have to go, how the lights should come on, and for how long.
Red light has to stay on for 20 seconds.
Than green light comes next, for 16 seconds.
Than yellow light comes after that, for 4 seconds.
Than the entire thing has to loop again, starting from red until the user clicks STOP.
With my earlier timer assignment, and this one, they seem to be closely related and takes about the same thing and looks to be done the same way. But, I was wrong in thinking that OR I'm doing something wrong with the timers.
When I click the start button, the red timer is enabled. Here is the timer settings. I specified these settings in the design view under the timer settings. So I didn't write it in the code area.
TM = Timer
R=Red
G=Green
Y=Yellow
TMR=TimerRed
TMG=TimerGreen
TMY=TimerYellow
TMR.Interval = 20000 '(20 seconds)
TMG.Interval = 16000 '(16 seconds)
TMY.Interval = 4000 '(4 seconds)
I also have 4 images on the form. And here are their names.
TL=Traffic Light
TLR = Traffic Light Red
TLG = Traffic Light Green
TLY = Traffic Light Yellow
TL is on top, and everything is blank, than TLR is the same TL image, except with a red circle for the red light, and so on.
So TMR HAS to display TLR for 20 seconds, than go on to TMG and display TLG for 16 seconds, than go on to TMY and display TLY for 4 seconds, than start again at TMR and display TLR for 20 seconds, etc.....
But it's not working like that. I coded my start button to display TLR AND start the TMR timer, which again, is set to display TLR again. The reason I told my button to display TLR is because when I start the TMR timer, it waits 16-20 seconds before it starts displaying TLR...but I wanted TLR to be displayed once the button was clicked, so it doesn't throw the user off thinking it doesn't work.
So, here is what the program is actually doing. When I click start, TMR starts, BUT, TLR is displayed for 16 seconds, TMG is displayed for 4 seconds, and TLY is displayed for 20 seconds. The timer interval from Red went to yellow, from yellow it went to green, and from green it went to red. So basically, the timer intervals went one up. I can't figure out why it did that.
I than tried using a label and colors and doing the same thing, instead of using images. And yet again, same problem. BUT, in my earlier program, everything worked fine, the color changed in the right order.
So here is my source code for the program.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TLR As System.Windows.Forms.PictureBox
Friend WithEvents TLY As System.Windows.Forms.PictureBox
Friend WithEvents TLG As System.Windows.Forms.PictureBox
Friend WithEvents TL As System.Windows.Forms.PictureBox
Friend WithEvents btnStart As System.Windows.Forms.Button
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
Friend WithEvents mnuExit As System.Windows.Forms.MenuItem
Friend WithEvents tmr As System.Windows.Forms.Timer
Friend WithEvents tmg As System.Windows.Forms.Timer
Friend WithEvents tmy As System.Windows.Forms.Timer
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Friend WithEvents Timer2 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.TLR = New System.Windows.Forms.PictureBox
Me.TLY = New System.Windows.Forms.PictureBox
Me.TLG = New System.Windows.Forms.PictureBox
Me.TL = New System.Windows.Forms.PictureBox
Me.btnStart = New System.Windows.Forms.Button
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.mnuFile = New System.Windows.Forms.MenuItem
Me.mnuExit = New System.Windows.Forms.MenuItem
Me.tmr = New System.Windows.Forms.Timer(Me.components)
Me.tmg = New System.Windows.Forms.Timer(Me.components)
Me.tmy = New System.Windows.Forms.Timer(Me.components)
Me.Button1 = New System.Windows.Forms.Button
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.Timer2 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'TLR
'
Me.TLR.BackgroundImage = CType(resources.GetObject("TLR.BackgroundImage"), System.Drawing.Image)
Me.TLR.Location = New System.Drawing.Point(48, 16)
Me.TLR.Name = "TLR"
Me.TLR.Size = New System.Drawing.Size(72, 160)
Me.TLR.TabIndex = 0
Me.TLR.TabStop = False
'
'TLY
'
Me.TLY.BackgroundImage = CType(resources.GetObject("TLY.BackgroundImage"), System.Drawing.Image)
Me.TLY.Location = New System.Drawing.Point(48, 16)
Me.TLY.Name = "TLY"
Me.TLY.Size = New System.Drawing.Size(72, 160)
Me.TLY.TabIndex = 1
Me.TLY.TabStop = False
'
'TLG
'
Me.TLG.BackgroundImage = CType(resources.GetObject("TLG.BackgroundImage"), System.Drawing.Image)
Me.TLG.Location = New System.Drawing.Point(48, 16)
Me.TLG.Name = "TLG"
Me.TLG.Size = New System.Drawing.Size(72, 160)
Me.TLG.TabIndex = 2
Me.TLG.TabStop = False
'
'TL
'
Me.TL.BackgroundImage = CType(resources.GetObject("TL.BackgroundImage"), System.Drawing.Image)
Me.TL.Location = New System.Drawing.Point(48, 16)
Me.TL.Name = "TL"
Me.TL.Size = New System.Drawing.Size(72, 160)
Me.TL.TabIndex = 3
Me.TL.TabStop = False
'
'btnStart
'
Me.btnStart.Location = New System.Drawing.Point(464, 40)
Me.btnStart.Name = "btnStart"
Me.btnStart.TabIndex = 4
Me.btnStart.Text = "Start"
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
'
'mnuFile
'
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuExit})
Me.mnuFile.Text = "File"
'
'mnuExit
'
Me.mnuExit.Index = 0
Me.mnuExit.Text = "Exit"
'
'tmr
'
Me.tmr.Interval = 20000
'
'tmg
'
Me.tmg.Interval = 16000
'
'tmy
'
Me.tmy.Interval = 4000
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(512, 136)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 5
Me.Button1.Text = "Button1"
'
'Timer1
'
Me.Timer1.Interval = 2000
'
'Timer2
'
Me.Timer2.Interval = 2000
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(632, 278)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.btnStart)
Me.Controls.Add(Me.TL)
Me.Controls.Add(Me.TLG)
Me.Controls.Add(Me.TLY)
Me.Controls.Add(Me.TLR)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Traffic Light"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
'hides TL and displayed TR
TL.Visible = False
TLR.Visible = True
'starts the TMG timer
tmg.Enabled = True
End Sub
Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
'closes the program
Dim splash As New splash
Me.Close()
splash.Close()
End Sub
Private Sub tmr_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick
'displays TLR and hides everything else
TLY.Visible = False
TLG.Visible = False
TLR.Visible = True
'disables this timer and starts TMG
tmr.Enabled = False
tmg.Enabled = True
End Sub
Private Sub tmg_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmg.Tick
'displays TLG and hides everything else
TLY.Visible = False
TLR.Visible = False
TLG.Visible = True
'disables this timer and starts TMY
tmg.Enabled = False
tmy.Enabled = True
End Sub
Private Sub tmy_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmy.Tick
'displays TLY and hides everything else
TLR.Visible = False
TLG.Visible = False
TLY.Visible = True
'disables this timer and starts TMR
tmy.Enabled = False
tmr.Enabled = True
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'when the form loads, all images except TL are hidden
TLR.Visible = False
TLG.Visible = False
TLY.Visible = False
End Sub
End Class
So far, I've been doing great in programming, in my class, I'm the fastest programmer and easily solve other people's problems, but this problem has really cut me back So can anyone help me? If you need me to clarify my problem more, or provide the images to the application, or anything else. Let me know and I'll be happy to provide anything
Thanks for reading and helping
-PcPro12