Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 17th, 2005, 09:16 PM
cassandra.flowers
Guest
 
Posts: n/a
Default Help with timers.

Hi,
I'm using Visual Basic 6 and I'm trying to make a program to change the
colours of traffic lights.

I am using 3 oval shapes, for each light. I want the traffic lights to
change colour through the standard traffic light sequence by changing the
'backcolor' of each oval: Red, Red & Amber, Green, Amber, Red.

There are 2 ways I am trying to do this:

With 1 command button. Each time it is clicked it changes the backcolor
property of the circles. The problem with this, is that I don't know how to
make something change each time the command button is clicked:

e.g. Red light
CLICK
Red & Amber light
CLICK
Green Light
CLICK, etc.

I tried doing it, but it changes all the lights at once, on the first click,
and I want each light to change each time the button is clicked. I know I
could do this using a few buttons, but don't know how to do it with just 1?

The second way I am trying to do this is using a Timer Control.

The problem is, I have no idea how to do this. All my reference books tell
me how to set it up to perform the 'TIME' function. Nothing else. I want
the lights to change after each second (1000 interval) but, I don't know how
to do this.

Could anyone shed some light on the subject please? I would be very
grateful for any help anyone can give.

Thanks

Cassandra


  #2  
Old July 17th, 2005, 09:16 PM
CMiYC
Guest
 
Posts: n/a
Default Re: Help with timers.

On Mon, 10 Nov 2003 18:23:59 +0000, cassandra.flowers wrote:[color=blue]
> With 1 command button. Each time it is clicked it changes the backcolor
> property of the circles. The problem with this, is that I don't know how to
> make something change each time the command button is clicked:
>
> e.g. Red light
> CLICK
> Red & Amber light[/color]


You need to setup a state machine. Then each time the
button is clicked you check the current "state", change the appropriate
colors, change state, and exit.

You'll need something like this:

onFormLoad()
state = 0 '0 = Red Light (initial state)


onButtonClick()
if state = 0 then
redOval.color = black
greenOval.color = green
end if

if state = 1 .... ' 1 = green
if state = 2 .... ' 2 = yellow

state = state + 1
if state = 3 then state = 0 ' start over

(obviously that's not the exact code but it should get you going)

Setting up a timer would replace onButtonClick() with onTimerTick or
whatever the Timer uses.

-James
  #3  
Old July 17th, 2005, 09:16 PM
Roy Riddex
Guest
 
Posts: n/a
Default Re: Help with timers.


"CMiYC" <cmiyc@nowhere.com> wrote in message
news:pan.2003.11.10.19.11.25.920162@nowhere.com...[color=blue]
> On Mon, 10 Nov 2003 18:23:59 +0000, cassandra.flowers wrote:[color=green]
> > With 1 command button. Each time it is clicked it changes the backcolor
> > property of the circles. The problem with this, is that I don't know[/color][/color]
how to[color=blue][color=green]
> > make something change each time the command button is clicked:
> >
> > e.g. Red light
> > CLICK
> > Red & Amber light[/color][/color]

I have recently tackled the exact same 2 programs from a book called
"Computing Projects in Visual Basic" by D. Christopher. I'm in a good mood
so I'll post my two programs for you.
This one is for the Click event program:

Option Explicit
Dim counter As Integer

Private Sub Command1_Click()
counter = counter + 1
If counter = 5 Then
counter = counter - 4
End If
If counter = 1 Then
shpRed.BackColor = RGB(255, 0, 0)
shpAmber.BackColor = RGB(255, 255, 255)
shpGreen.BackColor = RGB(255, 255, 255)
ElseIf counter = 2 Then
shpAmber.BackColor = RGB(210, 100, 45)
ElseIf counter = 3 Then
shpGreen.BackColor = RGB(0, 140, 0)
shpRed.BackColor = RGB(255, 255, 255)
shpAmber.BackColor = RGB(255, 255, 255)
ElseIf counter = 4 Then
shpGreen.BackColor = RGB(255, 255, 255)
shpAmber.BackColor = RGB(210, 100, 45)
End If
End Sub

Private Sub Form_Load()
counter = 1
shpRed.BackColor = RGB(255, 0, 0)
shpAmber.BackColor = RGB(255, 255, 255)
shpGreen.BackColor = RGB(255, 255, 255)
End Sub

and this one is for the timer controlled program:

Option Explicit
Dim counter As Integer

Private Sub Command1_Click()
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
counter = 1
shpRed.BackColor = RGB(255, 0, 0)
shpAmber.BackColor = RGB(255, 255, 255)
shpGreen.BackColor = RGB(255, 255, 255)
End Sub

Private Sub Timer1_Timer()
counter = counter + 1
If counter = 5 Then
counter = counter - 4
End If
If counter = 1 Then
shpRed.BackColor = RGB(255, 0, 0)
shpAmber.BackColor = RGB(255, 255, 255)
shpGreen.BackColor = RGB(255, 255, 255)
ElseIf counter = 2 Then
shpAmber.BackColor = RGB(210, 100, 45)
ElseIf counter = 3 Then
shpGreen.BackColor = RGB(0, 140, 0)
shpRed.BackColor = RGB(255, 255, 255)
shpAmber.BackColor = RGB(255, 255, 255)
ElseIf counter = 4 Then
shpGreen.BackColor = RGB(255, 255, 255)
shpAmber.BackColor = RGB(210, 100, 45)
End If
End Sub

To set the timer to an interval of 1000 open your form, select the timer
(which I assume you have placed on the form) then at the right of the screen
you can set the interval to 1000. Let me know how you get on.
PS. if you're gonna use my coding then remember to use the same names for
your lights i.e. shpGreen, shpRed, shpAmber


  #4  
Old July 17th, 2005, 09:16 PM
Roy Riddex
Guest
 
Posts: n/a
Default Re: Help with timers.

Oh and for the timed version, remember to have a command button to start
things off (you only need it if you 'borrow' my code from above).


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles