I am trying to create a set of traffic lights using visual c#, my problem is how to implement a timer between each light. I have 3 traffic lights and 2 pedestrian lights. Any help on the timer folks.
Printable View
I am trying to create a set of traffic lights using visual c#, my problem is how to implement a timer between each light. I have 3 traffic lights and 2 pedestrian lights. Any help on the timer folks.
Look at the Timer class..... :rolleyes: :rolleyes:
where's that Mr Vague
Did you ever consider a really radical approach of going to the MSDN site and typing in "Timer"...The very first hit is what you need.Quote:
Originally Posted by johnboy14
In case that is too difficult:http://msdn.microsoft.com/en-us/libr...rms.timer.aspx
LOL :D You guys crack me up! :D
very useful, I am new to c# so all and visual c# so I ain't quite up todate on all the sites and best places for different stuff. Thanks anyway.
maybe you should also try to use the little known search site called Google. a simple search for "c# timers" would have netted you plenty of results.Quote:
Originally Posted by johnboy14
msdn.microsoft.com is the official site for any Microsoft product related questions.
google.com is also a useful resource, but one needs to be careful since the results can come from "anyone". There is alot of incorrect material on the internet, and much of the .Net postings contain incorrect or sub-optimal information.
I usually recommmend starting with msdn then google if necessary. If you find something that looks good on google, you will have specific keywords that you can use to confirm the information back on msdn.
Would I declare the timer before the main or as soon as I enter the main.
You see I have a Button that activates the lights, I want it to wait 5 seconds then give me an amber light.
You "Declare" the timer by dragging it from the toolbox onto your form. You can initialize the duration in the properties window, or in code. You can start and stop the timer with the enabled property. You write an event handler which is triggered when the timer expires.
Did you download and step through the available samples?
bits of it, I am familar with declaring variables and methods etc. but I programmed a set of traffic lights with an AVR board once in C but low level stuff to be honest. I'll read through them,
p.s I was unaware that there was a timer on visual c#, so I could drag and drop. I can't seem to find it on my toobar though.
In all honesty I'm not sure how to start.
this below is my code for my start button, I set the timer to intervals of 5 seconds so am I free to place what what I want my lights to do now.
private void button1_Click(object sender, System.EventArgs e)
{
timer1.Enabled = true
Go back to design mode on your form. Select the timer. Select the Properties Windows. Switch to events view (looks like a lightning bolt). Double click on the event. This will add an empty routine to your code where you put the logic of what to do when the timer expires...
I will ask one more time...
Did you download and step through the available samples?
No I Did Not.
I want it to just run in a loop. until the button is pressed
Then do so. If you have specific problems understanding someting in one of the turorials, plase post the URL you got the tutorial from and your question.Quote:
Originally Posted by johnboy14
Until then....
http://msdn.microsoft.com/en-us/library/3tszykws.aspx
I'm using this tutorial, not I can't figure out what to put in my timer event.
I want to press button1
wait 5 seconds
and the world "ON" should be placed in the text box of my choice
wait another 5
and the word "ON" is allocated as sequence of traffic lights.
Press button. In click even enable the timer. In the timer_tick method look to see if the light is on, if so turn it off, else turn it in.
Thats it
[QUOTE=In the timer_tick method look to see if the light is on, if so turn it off, else turn it in.
Thats it[/QUOTE]
I don't know what your talking about here mate.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeTimer()
{
//' Run this procedure in an appropriate event.
// Set to 5 second.
timer1.Interval = 5000;
// Enable timer.
timer1.Enabled = true;
StartButton.Text = "Stop";
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
red.Text = ("OFF");
amber.Text = ("ON");
green.Text = ("OFF");
}
}
}
1) Please use [ code ] tags. and use preview, so you dont have messed up posts.
There are more efficient way using enums, and arrays, but this will work (sucject to typos, but you should get the ides....Code:private void timer1_Tick(object sender, EventArgs e)
{
if (red.Text == "ON")
{
red.Text=="OFF"
green.Text = "ON"
}
else if (green.Text == "ON")
{
green.Text = "OFF";
amber.Text = "ON";
}
else
{
amber.Text = "OFF";
red.Text = "ON";
}
}
that makes perfect sense, using bool expressions. Thanks again, I don't mean to be a dump *** but its all part of my learning methods. http://www.codeguru.com/forum/images/icons/icon10.gif