This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I enable this timer in C#?
Im trying to get a little project running. When I use a break point it goes through the code correctly, but when running the program at normal speed it the sequence runs too fast. Im trying to get the traffic lights sequence to change every 1 second. What is wrong with this code? Its a simple sequence of traffic lights, incase your interested :). Newbie project.
}
public int counter = 0;
private void rbStart_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Start();
counter++;
if (counter == 1)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else if (counter == 2)
{
pbRed.Visible = true;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 3)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = true;
}
else if (counter == 4)
{
pbRed.Visible = false;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 5)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else
{
counter = 0;
}
}
private void rbStop_CheckedChanged(object sender, EventArgs e)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
Light_timer.Tick += new EventHandler(rbStart_CheckedChanged);
Light_timer.Interval = 1000;
Light_timer.Stop();
}
}
}
You're hooking up the event handler every time the timer elapses and so on... Try this:
private void Form1_Load(object sender, EventArgs e)
{
Light_timer = new Timer();
Light_timer.Tick += new EventHandler(TimerElapsed);
Light_timer.Interval = 1000;
}
private void TimerElapsed(object sender, EventArgs e)
{
counter++;
if (counter == 1)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else if (counter == 2)
{
pbRed.Visible = true;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 3)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = true;
}
else if (counter == 4)
{
pbRed.Visible = false;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 5)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else
{
counter = 0;
Light_timer.Stop();
}
}
private void rbStart_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Start();
}
private void rbStop_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Stop();
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
Related
In a Winforms application, there are 3 different numericupdown such as min, sec, millisecond. How do I make a timer that counts down the value of entering numericupdowns? I have tried with if else blocks. I also saw a lot of time timespawn titles on the internet. Which is better for this countdown? if else blocks or timespawn
numericUpDownMiliSn.Value--;
if (numericUpDownMiliSn.Value == 0)
{
if (numericUpDownMiliSn.Value == 0 && numericUpDownSn.Value == 0 && numericUpDownDk.Value == 0)
{
timer2.Stop();
button2.Text = "Baslat";
durum = false;
}
else
{
if (numericUpDownSn.Value > 0)
{
numericUpDownSn.Value--;
numericUpDownMiliSn.Value = 60;
}
else
{
numericUpDownMiliSn.Value = 60;
}
if (numericUpDownSn.Value > 0)
{
numericUpDownSn.Value--;
numericUpDownSn.Value = 60;
}
}
}
From my comments in the original question above:
Timers in WinForms are NOT accurate, so you shouldn't be basing your
time off incrementing/decrementing those in a Tick() event. You should
definitely be using a TimeSpan (derived from subtracting the current
time from some future target time; based on the initial values in your
NumericUpDowns)...then simply update the NumericUpDowns with the
numbers in the TimeSpan.
Here's how that code might look:
private DateTime targetDT;
private void button1_Click(object sender, EventArgs e)
{
TimeSpan ts = new TimeSpan(0, 0, (int)numericUpDownMn.Value, (int)numericUpDownSn.Value, (int)numericUpDownMiliSn.Value);
if (ts.TotalMilliseconds > 0)
{
button1.Enabled = false;
numericUpDownMn.Enabled = false;
numericUpDownSn.Enabled = false;
numericUpDownMiliSn.Enabled = false;
targetDT = DateTime.Now.Add(ts);
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = targetDT.Subtract(DateTime.Now);
if (ts.TotalMilliseconds > 0)
{
numericUpDownMn.Value = ts.Minutes;
numericUpDownSn.Value = ts.Seconds;
numericUpDownMiliSn.Value = ts.Milliseconds;
}
else
{
timer1.Stop();
numericUpDownMn.Value = 0;
numericUpDownSn.Value = 0;
numericUpDownMiliSn.Value = 0;
button1.Enabled = true;
numericUpDownMn.Enabled = true;
numericUpDownSn.Enabled = true;
numericUpDownMiliSn.Enabled = true;
}
}
I've made an countdown and i want to add an time check for it now. If the minutes are < 01 and the seconds are != 60 so 00:59 the Time should be orange and if the seconds are then smaller then 10 the time should be red.
But it does not work.
They're always just getting orange if the time is 00:00:58, but why?
private int hours, minutes, seconds;
private bool paused;
private void button_Start_Click(object sender, EventArgs e)
{
button_Pause.Enabled = true;
button_Stop.Enabled = true;
if(paused != true)
{
hours = int.Parse(textBox_Hours.Text);
minutes = int.Parse(textBox_Minutes.Text);
seconds = int.Parse(textBox_Seconds.Text) + 1;
textBox_Hours.Enabled = false;
textBox_Minutes.Enabled = false;
textBox_Seconds.Enabled = false;
button_Start.Enabled = false;
timer_CountDown.Start();
}
}
private void timer_CountDown_Tick(object sender, EventArgs e)
{
if(hours == 0 && minutes < 1)
{
label_Hours.ForeColor = Color.Red;
label_Minutes.ForeColor = Color.Red;
label_Seconds.ForeColor = Color.Red;
label8.ForeColor = Color.Red;
label10.ForeColor = Color.Red;
}
if(hours == 0 && minutes == 0 && seconds == 0)
{
timer_CountDown.Stop();
textBox_Seconds.Enabled = true;
textBox_Minutes.Enabled = true;
textBox_Hours.Enabled = true;
button_Start.Enabled = true;
}
else
{
if (seconds < 1)
{
seconds = 59;
if (minutes < 1)
{
minutes = 59;
if (hours != 0)
{
hours -= 1;
}
}
else
{
minutes -= 1;
}
}
else
{
seconds -= 1;
}
if(hours > 9)
{
label_Hours.Text = hours.ToString();
}
else { label_Hours.Text = "0" + hours.ToString(); }
if(minutes > 9)
{
label_Minutes.Text = minutes.ToString();
}
else { label_Minutes.Text = "0" + minutes.ToString(); }
if(seconds > 9)
{
label_Seconds.Text = seconds.ToString();
}
else { label_Seconds.Text = "0" + seconds.ToString(); }
}
}
The Timer Intervall is 1000.
You're over complicating things. Why not just use the TimeSpan type and get rid of those hours, minutes, seconds?
private TimeSpan countDownTime = TimeSpan.Zero;
private void timer_CountDown_Tick(object sender, EventArgs e)
{
if(countDownTime == TimeSpan.Zero)
{
timer_CountDown.Stop();
textBox_Seconds.Enabled = true;
textBox_Minutes.Enabled = true;
textBox_Hours.Enabled = true;
button_Start.Enabled = true;
return;
}
countDownTime = countDownTime.Add(TimeSpan.FromSeconds(1).Negate());
label_Hours.Text = countDownTime.ToString("hh");
label_Minutes.Text = countDownTime.ToString("mm");
label_Seconds.Text = countDownTime.ToString("ss");
if(countDownTime.TotalSeconds < 10)
{
label_Hours.ForeColor = Color.Red;
label_Minutes.ForeColor = Color.Red;
label_Seconds.ForeColor = Color.Red;
label8.ForeColor = Color.Red;
label10.ForeColor = Color.Red;
}
else if (countDownTime.TotalMinutes < 1)
{
label_Hours.ForeColor = Color.Orange;
label_Minutes.ForeColor = Color.Orange;
label_Seconds.ForeColor = Color.Orange;
label8.ForeColor = Color.Orange;
label10.ForeColor = Color.Orange;
}
}
private void button_Start_Click(object sender, EventArgs e)
{
button_Pause.Enabled = true;
button_Stop.Enabled = true;
if(paused != true)
{
int hours = int.Parse(textBox_Hours.Text);
int minutes = int.Parse(textBox_Minutes.Text);
int seconds = int.Parse(textBox_Seconds.Text) + 1;
this.countDownTime = new TimeSpan(hours,minutes,seconds);
textBox_Hours.Enabled = false;
textBox_Minutes.Enabled = false;
textBox_Seconds.Enabled = false;
button_Start.Enabled = false;
timer_CountDown.Start();
}
}
Is there a way to group 30+ labels to be able to control them all at once. What I want to do is this with 30 labels.
if (player.Bounds.IntersectsWith(label1.Bounds))
{
if (right == true)
{
right = false;
left = true;
}
else if (left == true)
{
left = false;
right = true;
}
else if (up == true)
{
up = false;
down = true;
}
else if (down == true)
{
down = false;
up = true;
}
And then where the label1 is checking if it is colided I want it to check all 30 labels if they have colided. And preferably not with 30x this code and just change the number. =)
I just want to add this is a maze game and the left, right etc. is the players movement defined outside what I posted here. I hope you understand!
All of my code:
namespace mazeGame
{
public partial class Form1 : Form
{
bool down;
bool left;
bool right;
bool up;
// new List<int> blocks = new List[5];
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (label1.Bounds.IntersectsWith(label10.Bounds))
{
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
down = false;
up = false;
right = true;
left = false;
}
if (e.KeyCode == Keys.Left)
{
left = true;
down = false;
up = false;
right = false;
}
if (e.KeyCode == Keys.Up)
{
up = true;
down = false;
right = false;
left = false;
}
if (e.KeyCode == Keys.Down)
{
down = true;
up = false;
right = false;
left = false;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
right = true;
left = false;
up = false;
down = false;
}
if (e.KeyCode == Keys.Left)
{
left = true;
right = false;
up = false;
down = false;
}
if (e.KeyCode == Keys.Up)
{
up = true;
left = false;
right = false;
down = false;
}
if (e.KeyCode == Keys.Down)
{
down = true;
left = false;
up = false;
right = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (player.Bounds.IntersectsWith(label1.Bounds))
{
if (right == true)
{
right = false;
left = true;
}
else if (left == true)
{
left = false;
right = true;
}
else if (up == true)
{
up = false;
down = true;
}
else if (down == true)
{
down = false;
up = true;
}
}
var labels = this.??????? // here is where i need help.
if (right == true)
{
player.Left += 1;
}
if (left == true)
{
player.Left -= 1;
}
if (up == true)
{
player.Top -= 1;
}
if (down == true)
{
player.Top += 1;
}
}
}
OfType will get all controls of the same type from a control. In this case it will be labels from your form:
var labels = this.myForm.Controls.OfType<Label>()
Then you can iterate through your collection of labels.
EDIT:
Then, looping through your code it would look like this:
private void timer1_Tick(object sender, EventArgs e)
{
var labels = this.Form1.Controls.OfType<Label>()
foreach(var label in labels)
{
if(player.Bounds.IntersectsWith(label.Bounds))
//...
As for me it is good decision to create array or even list (witch you can dynamically update) of all your 30+ labels and then to manipulate them in one short method. Of course, if the actions as much simple as you write up and nothing else.
That's my code below and it works perfectly fine, except there is one thing that doesn't work correctly. When I click two buttons to match, they should stay visible until the user clicks a third button. How can I do that? Thank you.
namespace Memorija_Seminarska
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tableLayoutPanel1.Enabled = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
progressBar1.Visible = false;
label2.Text = vreme.ToString();
}
public int vreme = 150;
Random random = new Random();
Button firstClicked = null;
Button secondClicked = null;
List<string> drzava = new List<string>()
{
"Македонија","Македонија", "Бугарија","Бугарија", "Србија","Србија",
"Германија","Германија", "Канада","Канада", "Шпанија","Шпанија",
"Португалија","Португалија", "Австрија","Австрија", "Данска","Данска",
"Индија","Индија", "Италија","Италија", "Англија","Англија",
"Турција","Турција", "Грција","Грција","Хрватска","Хрватска",
"Холандија","Холандија", "Русија", "Русија", "Швајцарија","Швајцарија"
};
private void startButton_Click(object sender, EventArgs e)
{
Add();
tableLayoutPanel1.Enabled = true;
label1.Visible = true;
label2.Visible = true;
progressBar1.Visible = true;
timer2.Start();
timer3.Start();
}
private void Add()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button b = control as Button;
if (b != null)
{
int randNum = random.Next(drzava.Count);
b.Text = drzava[randNum];
b.ForeColor = b.BackColor;
drzava.RemoveAt(randNum);
}
}
}
private void button_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
return;
Button clickedButton = sender as Button;
if (clickedButton != null)
{
if (clickedButton.ForeColor == Color.Black)
return;
if (firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;
Win();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked.BackColor = Color.GreenYellow;
secondClicked.BackColor = Color.GreenYellow;
firstClicked = null;
secondClicked = null;
return;
}
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
private void timer2_Tick(object sender, EventArgs e)
{
vreme--;
label2.Text = vreme.ToString();
if (vreme == 0)
{
tableLayoutPanel1.Enabled = false;
label3.Text = "Game over!";
label3.Visible = true;
label2.Visible = false;
timer2.Stop();
timer3.Stop();
label1.Visible = false;
progressBar1.Visible = false;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
progressBar1.Value -= 1;
if (progressBar1.Value == 0)
{
timer3.Stop();
}
}
private void Win()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Button button1 = control as Button;
if (button1 != null)
{
if (button1.ForeColor == button1.BackColor)
{
return;
}
}
}
label3.Text = "Браво!!!";
label3.Visible = true;
tableLayoutPanel1.Enabled = false;
timer2.Stop();
timer3.Stop();
label2.Visible = false;
progressBar1.Visible = false;
label1.Visible = false;
}
}
}
As far as I can see, your timer1_Tick handler performs the hiding automatically when it's time period expires. In case you want this hiding to happen manually when third card is clicked, you should not hide the buttons there, but should just perform a check in the beginning of button_Click:
private void button_Click(object sender, EventArgs e)
{
//two cards are open and not matching (if they matched, they would be already null)
if ( firstClicked != null && secondClicked != null )
{
//hide the buttons
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
}
And delete the timer1.Start() from the end of the event handler.
im not good at understanding other people code, but as far as i look through, you do this in here:
private void button_Click(object sender, EventArgs e)
Check for null
get reference of button 1 and return
get reference of button 2
do win() method
check for equality
here you should send step 5 to front line, check for null as MZetko said, and then check for equality, so it will be third click, else if you get reference as you did, after second button filled up, the checking will also launch
i hope i were helpful
Hi, I have code that reads and writes to a COM port. When the program reads from the COM port it searches for a string value and puts it in a variable. After it does this, it again listens to the COM port. I need to write to the COM port and read some new data, but I'm not seeing the value has changing to a new value.
Here is my code:
private void timer1_Tick(object sender, EventArgs e)
{
sq = "777";
if (CommunicationManager.myQ.Count != 0)
{
sq = CommunicationManager.myQ.Dequeue().ToString();
textBox1.Text = sq + textBox1.Text;
buffer = Regex.Match(textBox1.Text, #"\
((.+?)\,15,").Groups[1].Value;
}
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= numtst; i++)
{
listView1.Items[i].BackColor = Color.White;
fl[i] = false;
}
nt = 0;
flon = false;
flag[0] = false;
comm.WriteData("AT\r\n");
wait(700);
if (buffer.lenght == 16)
{
flag[0] = true
}
if (flag[0] == true)
{
flon = true;
CommunicationManager.myQ.Clear();
break;
}
}
if (flon == true)
{
listView1.Items[nt].BackColor = Color.LightGreen;
fl[nt] = true;
}
else
{
listView1.Items[nt].BackColor = Color.Red;
if (flag[76] == true)
{
button1.Enabled = true;
button1.BackColor = Color.Red;
button1.Text = "Test ERROR";
return;
}
}
comm.WriteData("ATT\r\n");
wait(3700);
comm.WriteData("AT4\r\n");
nt = 1;
flon = false;
flag[1] = false;
if (buffer == text4.text)
{
flag[1] = true
}
wait(700);
if (flag[1] == true)
{
flon = true;
CommunicationManager.myQ.Clear();
break;
}
if (flag[76] == true)
{
button1.Enabled = true;
return;
}
}
if (flon == true)
{
listView1.Items[nt].BackColor = Color.LightGreen;
fl[nt] = true;
}
else
{
listView1.Items[nt].BackColor = Color.Red;
if (flag[76] == true)
{
button1.Enabled = true;
button1.BackColor = Color.Red;
button1.Text = "Test ERROR";
return;
}
}
in the second part if (buffer == text4.text) i see only first value of the buffer variable.
i checked in the terminal and all commands working good.