I made a timer in code (not through drag and drop controls) and it says timer.tick is an event.
How do I use that as an event and have it perform actions in that event?
such as:
Code:timer.tick { //code here }
I made a timer in code (not through drag and drop controls) and it says timer.tick is an event.
How do I use that as an event and have it perform actions in that event?
such as:
Code:timer.tick { //code here }
From MSDN:
Code:// C# // This variable will be the loop counter. private int counter; private void InitializeTimer() { // Run this procedure in an appropriate event. counter = 0; timer1.Interval = 600; timer1.Enabled = true; // Hook up timer's tick event handler. this.timer1.Tick += new System.EventHandler(this.timer1_Tick); } private void timer1_Tick(object sender, System.EventArgs e) { if (counter >= 10) { // Exit loop code. timer1.Enabled = false; counter = 0; } else { // Run your procedure here. // Increment counter. counter = counter + 1; label1.Text = "Procedures Run: " + counter.ToString(); } }
To code is divine
main part of the code anyways but I get this error...Code:Timer timer = new Timer(); public Form1() { InitializeComponent(); //Timer set up timer.Interval = 100; this.timer.Tick += new EventHandler(timer_Tick); } }
Error 1 The name 'timer_Tick' does not exist in the current context C:\Documents and Settings\Alex\Desktop\Type Helper\Type Helper\Form1.cs 24 48
Do you have a function timer_Tick? Post all of your code please, to better get an understanding of the problem.
To code is divine
that is all my code, unless you want the visual studio genterated stuff that is all of the code.
So I guess I missing somthing.
An eventhandler is a call to a method that does something. When you call the
method, your telling the program to run a method everytime the timer ticks. The reason your code isn't compiling is because there is no method called timer_Tick for the eventhandler to call.Code:this.timer.Tick += new EventHandler(timer_Tick);
You can get rid of this problem by creating a method timer_Tick like this:
Code:public void timer_Tick(object sender, System.EventArgs e) { //code here }
Last edited by 7smurfs; 04-03-2005 at 07:16 AM.
To code is divine
ahh ha. Ok I get it now, thanks alot this has been bugging me for some days now.
No problem man, glad to see your getting the hang of it.
To code is divine