Tell me, does VS have a Timer component, there it has 1 Timer1_Tick event, so I didn’t quite understand when it worked?

And the second question, the Interval field, I did not quite understand how to fill it correctly ... I enter 1000 - it works every second, I enter 10,000 - it works only 1 time ...

  • one
    On the second question, the event will be executed every 10 seconds. Perhaps you just did not wait for the second response. Check it out. - Olter

1 answer 1

An event Timer_tick with a Timer_tick will be executed once per interval. The interval is counted in milliseconds. (i.e. interval = 1000 is 1 second)

Actually, on msdn it is all there .

Work code:

 using System; using System.Windows.Forms; namespace TimerTest { public partial class Form1 : Form { private int timer = 0; private bool flag=false; public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { timer++; lTimer.Text = timer.ToString(); } private void bStart_Click(object sender, EventArgs e) { if (flag == false) { flag = true; timer1.Enabled = true; timer1.Start(); timer++; } else { timer1.Stop(); timer1.Enabled= false; flag = false; } } } 

}

  • But the question is, I have a variable timer (of type int), and every 10 seconds it should increase by 1 .... but for some reason this happens only once, and then I waited for 3 minutes, and it no longer increased. Here is the code by which I tried to increase the variable (can it be enough) timer ++; - Angus123
  • Tax. By timer ++, I will not tell you anything. Full code timer_tick in the studio. upd: Okay, suppose you are working with a form. It should be like this: 1) The global variable timer is declared: private int timer = 0; 2) When the form is initialized, the timer variable is set to 0. 3) In timer_tick, write timer ++; // output the timer variable to the form 4) Yes, of course, the timer should be started. Suppose that this is done by clicking on a button: timer.enabled = true; timer.start (); Check it out. - Olter
  • And for some reason, it does not work for me ... tell me you have Skype? I just have a slightly different purpose for the timer ... It's easier to tell and show. - Angus123
  • one
    No, sorry, I will not give my Skype)). It is better to try to explain the essence of the problem, still hashcode and was created in order to solve problems. (if, of course, there is nothing top secret in your code) - Olter
  • one
    And why then flag variable? And it will always be false. Judging by the code, with a single press of a button, the timer starts, the next time it is pressed, it stops. Or not all the code here ... - krupennikov