I am trying to make very simple timer application. I do have a "text" object that's displaying the time after its being converted to string using: .ToString("hh\:mm\:ss");
Unfortunately after clicking the button for the whole event to start again - the if statements are being executed as if the timer was just repeating itself (keeping the old tick and text values in background) so if statements actions starts overlapping each other :(.
I have windows.forms.timer called timer2 placed in application. I have also a button called Button01 and text objects called Button01text1 & Button01textleft. The background colours and timer stop events are based on the text values comparison.
OLD CODE (invalid usage of string) :
private void Button01_click(object sender, EventArgs e)
{
var startTime = DateTime.Now;
Button01.BackColor = Color.FromName("Green");
Button01textleft.BackColor = Color.FromName("Green");
timer2.Tick += (obj, args) =>
{
Button01text1.Text =
(TimeSpan.FromMinutes(1) - (DateTime.Now - startTime))
.ToString("hh\\:mm\\:ss");
Button01textleft.Text =
(TimeSpan.FromMinutes(1) - (DateTime.Now - startTime))
.ToString("hh\\:mm\\:ss");
if (Button01text1.Text == "00:00:30")
{
Button01.BackColor = Color.FromName("Orange");
Button01textleft.BackColor = Color.FromName("Orange");
}
else if (Button01text1.Text == "00:00:00")
{
Button01.BackColor = Color.FromName("Red");
Button01textleft.BackColor = Color.FromName("Red");
timer2.Stop();
}
};
timer2.Enabled = true;
}
New code (same issue, but updated thanks to @Dleh):
public void Button01_Click(object sender, EventArgs e)
{
Timer timer1 = new System.Windows.Forms.Timer();
var startTime = DateTime.Now;
Button01text1.BackColor = Color.FromName("Green");
Button01textleft.BackColor = Color.FromName("Green");
timer1.Tick += (obj, args) =>
{
var now = DateTime.Now;
var timeDifference = (TimeSpan.FromSeconds(30) - (now - startTime));
var stringValue = timeDifference.ToString("hh\\:mm\\:ss");
Button01text1 = stringValue;
Button01textleft.Text = stringValue;
if (timeDifference <= TimeSpan.FromSeconds(15))
{
Button01text1.BackColor = Color.FromName("Orange");
Button01textleft.BackColor = Color.FromName("Orange");
}
else if (timeDifference <=TimeSpan.FromSeconds(0))
{
Button01text1.BackColor = Color.FromName("Red");
Button01textleft.BackColor = Color.FromName("Red");
timer1.Stop();
}
};
timer1.Enabled = true;
}
Now to give example what happens:
I press button once - it turns green, at 30 seconds left it turns orange at 00 it turns red and stops timer.
If I press button again in the middle of counting (at 40 seconds) it will turn to green & back to 60 seconds, but will change to orange at 50 seconds left (as if the previous tick still count down and reached 30 if I had not clicked the button again).
I am clueless, don't know why its happening - cause it's supposed to be checking the string text values - which shouldn't exist as separate instances...
Any ideas ?_?
Example Video of what's happening: Screen_recording
Maria