473,480 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
3 5978
On Mon, 10 Nov 2003 18:23:59 +0000, cassandra.flowers wrote:
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

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
Jul 17 '05 #2

"CMiYC" <cm***@nowhere.com> wrote in message
news:pa****************************@nowhere.com...
On Mon, 10 Nov 2003 18:23:59 +0000, cassandra.flowers wrote:
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


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
Jul 17 '05 #3
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).
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
371
by: GDumencu | last post by:
I have a C# program that have to run all the time. For some reasons I cannot make it a service. This program has 3 timers and by time to time ( days or weeks) one of them stops. Some times, starts...
0
1738
by: Dmitry Demchuk | last post by:
Hi everybody. Recently I ran into situation with System.Threading.Timer in my ASP.NET application. I reinstalled Windows on one of my servers and got timers stop firing events after while, they...
9
7841
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
10
2666
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
1
1803
by: | last post by:
Frustrated.. (I have seen other posts regarding this problem with no resolution..) I am using dotnet 1.1 with latest SP on a Win2KP box (actually 2 boxes), have even run the service on WinXP SP2...
1
2994
by: Jonathan Woods | last post by:
Hi there, I have three methods these need to execute at every interval time. I would like to know which option is better? Option A) Three System.Timers.Timer objects these execute each...
2
5952
by: kkb | last post by:
Hello! First, I'm sorry because of my english... I'll try to be understandable! I've got a strange problem using .NET 2003 C# and I haven't figured it out for a long time. I'm implementing an...
3
8604
by: Steve Lowe | last post by:
Hi, I'm getting into VB.net with the help of a few books, but have got a problem grasping how to implement a system.timers.timer Only 1 of my 3 books mentions timers and that's a...
11
1949
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
5
3266
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.