how to call function timer on button click
-
Hi I`m trying to run timer on tick method after clicking the button and i dont really understand how it can be done. can anyone help please :doh: ?? for example :
private void button1_Click(object sender, EventArgs e) { myTimer.Enabled = true; //Run myTimer_tick } private void myTimer_Tick(object sender, EventArgs e) { //Do something here }
-
Hi I`m trying to run timer on tick method after clicking the button and i dont really understand how it can be done. can anyone help please :doh: ?? for example :
private void button1_Click(object sender, EventArgs e) { myTimer.Enabled = true; //Run myTimer_tick } private void myTimer_Tick(object sender, EventArgs e) { //Do something here }
I suppose you are trying to do something like this:
private void button1\_Click(object sender, EventArgs e) { myTimer.Tick += new EventHandler(myTimer\_Tick); myTimer.Interval = 2000; // once every two seconds myTimer.Enabled = true; } void myTimer\_Tick(object sender, EventArgs e) { MessageBox.Show("tick"); }
-
I suppose you are trying to do something like this:
private void button1\_Click(object sender, EventArgs e) { myTimer.Tick += new EventHandler(myTimer\_Tick); myTimer.Interval = 2000; // once every two seconds myTimer.Enabled = true; } void myTimer\_Tick(object sender, EventArgs e) { MessageBox.Show("tick"); }
-
I suppose you are trying to do something like this:
private void button1\_Click(object sender, EventArgs e) { myTimer.Tick += new EventHandler(myTimer\_Tick); myTimer.Interval = 2000; // once every two seconds myTimer.Enabled = true; } void myTimer\_Tick(object sender, EventArgs e) { MessageBox.Show("tick"); }
you should not add the event handler in the button click event as you will get a new handler added each time you press the button. So in your example the message box will be shown the same number of times that the button has been clicked. Just need to take out the adding of the event handler and put that line in the design code, or constructor, or anywhere it will only be called once.
Life goes very fast. Tomorrow, today is already yesterday.
-
See my reply to the above answer if you find that it does not work as you want it too
Life goes very fast. Tomorrow, today is already yesterday.
I want to open Firefox ever hour!
void myTimer_Tick(object sender, EventArgs e) { System.diragnostic.process.start("firefox); } static void Listen() { while (true) { try { data = new byte[1024]; received = networkStream.Read(data, 0, data.Length); if (received == 0) break; string get = Encoding.ASCII.GetString(data, 0, received); var send = get.ToByteArray(); //Console.WriteLine("Received from TCP Client: " + get); networkStream.Write(send, 0, send.Length); if (get == "rstartFirefox") timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = 3600000; // once every two seconds timer1.Enabled = true;
here it doesn´t work! the code works great by a Button_Click! What can i do that it works here? -
I want to open Firefox ever hour!
void myTimer_Tick(object sender, EventArgs e) { System.diragnostic.process.start("firefox); } static void Listen() { while (true) { try { data = new byte[1024]; received = networkStream.Read(data, 0, data.Length); if (received == 0) break; string get = Encoding.ASCII.GetString(data, 0, received); var send = get.ToByteArray(); //Console.WriteLine("Received from TCP Client: " + get); networkStream.Write(send, 0, send.Length); if (get == "rstartFirefox") timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = 3600000; // once every two seconds timer1.Enabled = true;
here it doesn´t work! the code works great by a Button_Click! What can i do that it works here?start by moving the following two lines to your constructor, and use it no where else
timer1.Tick += new EventHandler(myTimer_Tick);
timer1.Interval = 3600000then after all the code you have posted add the following line to check what going on..
MessageBox.Show("get = " + get);
let me know what pops up and how many times.
Life goes very fast. Tomorrow, today is already yesterday.
-
start by moving the following two lines to your constructor, and use it no where else
timer1.Tick += new EventHandler(myTimer_Tick);
timer1.Interval = 3600000then after all the code you have posted add the following line to check what going on..
MessageBox.Show("get = " + get);
let me know what pops up and how many times.
Life goes very fast. Tomorrow, today is already yesterday.
Nothing! Now i have it so! I get the massage box! Open also takes the correct value! But he don´t open the Methode myTimer_Tick! What can i do?
public Form1() { InitializeComponent(); } void myTimer_Tick(object sender, EventArgs e) { mciSendString("set CDAudio door open", null, 127, (IntPtr)0); }
if (get.Contains("rRegelmaessig_oeffnen")) { Messagebox.show("Hello"); string[] get_Split2 = get.Split('~'); int open = Convert.ToInt32(get_Split2[1]); timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = open * 1000; timer1.Enabled = true; }
-
Nothing! Now i have it so! I get the massage box! Open also takes the correct value! But he don´t open the Methode myTimer_Tick! What can i do?
public Form1() { InitializeComponent(); } void myTimer_Tick(object sender, EventArgs e) { mciSendString("set CDAudio door open", null, 127, (IntPtr)0); }
if (get.Contains("rRegelmaessig_oeffnen")) { Messagebox.show("Hello"); string[] get_Split2 = get.Split('~'); int open = Convert.ToInt32(get_Split2[1]); timer1.Tick += new EventHandler(myTimer_Tick); timer1.Interval = open * 1000; timer1.Enabled = true; }