Strange problem with display time
-
I all. I have a strange case with display timer my code look like this: Timer myTimer = new Timer(); public static int nr = 1; private void button1_Click(object sender, EventArgs e) { myTimer.Interval = 1000; myTimer.Enabled = true; myTimer.Start(); myTimer.Tick += new EventHandler(myTimer_Tick); } void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); } But if i click on a stop button with:(myTimer.Stop()), then when i press on play button again the string (nr) has change to 2 and next time 3 How do i make this work?? Anyone with a good idea?? Tnx!!!
-
I all. I have a strange case with display timer my code look like this: Timer myTimer = new Timer(); public static int nr = 1; private void button1_Click(object sender, EventArgs e) { myTimer.Interval = 1000; myTimer.Enabled = true; myTimer.Start(); myTimer.Tick += new EventHandler(myTimer_Tick); } void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); } But if i click on a stop button with:(myTimer.Stop()), then when i press on play button again the string (nr) has change to 2 and next time 3 How do i make this work?? Anyone with a good idea?? Tnx!!!
Almost no textbook or college course teaches you to use a debugger. But you should. Try selecting the first line in myTimer_Tick(), hit F9 to set a breakpoint, then run the program. When it stops there, put your mouse over the variables or look at the Watch box to see what the values are on the variables. Then execute one line at a time (Debug -> Step Over) and figure out what's wrong. It's a really good way to fix problems and learn programming. By the way, what exactly are you trying to do?
-
I all. I have a strange case with display timer my code look like this: Timer myTimer = new Timer(); public static int nr = 1; private void button1_Click(object sender, EventArgs e) { myTimer.Interval = 1000; myTimer.Enabled = true; myTimer.Start(); myTimer.Tick += new EventHandler(myTimer_Tick); } void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); } But if i click on a stop button with:(myTimer.Stop()), then when i press on play button again the string (nr) has change to 2 and next time 3 How do i make this work?? Anyone with a good idea?? Tnx!!!
First of all : Avoid declaring many variables. So Instead of
void myTimer_Tick(object sender, EventArgs e) { string a = ""; int tid1 = 0; int tid2 = 0; a = label21.Text; tid1 = Convert.ToInt32(a); tid2 = (int)tid1 - nr; label21.Text = tid2.ToString(); }
You can just use which is more readable.void myTimer_Tick(object sender, EventArgs e) { label21.Text = (Convert.ToInt32(label21.Text) - nr).ToString(); }
We can understand that you are decrementing a number and displaying it in a textBox. Your question is "string (nr) has change to 2 and next time 3". - nr is not a string, nr is a static int So what do you mean by this? Do you mean the text is being incremented instead of decrementing. Well this is impossible as you see that there is no addition code in myTimer_Tick(). Best Regards.