Thread: timer.tick huh?

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    timer.tick huh?

    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
    }

  2. #2
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    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

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Code:
            Timer timer = new Timer();
    
            public Form1()
            {
                InitializeComponent();
    
                //Timer set up
                timer.Interval = 100;
                this.timer.Tick += new EventHandler(timer_Tick);
            }
        }
    main part of the code anyways but I get this error...

    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

  4. #4
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    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

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    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.

  6. #6
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    An eventhandler is a call to a method that does something. When you call the
    Code:
    this.timer.Tick += new EventHandler(timer_Tick);
    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.
    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

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ahh ha. Ok I get it now, thanks alot this has been bugging me for some days now.

  8. #8
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    No problem man, glad to see your getting the hang of it.
    To code is divine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random crashes? huh?
    By cpjust in forum C++ Programming
    Replies: 28
    Last Post: 07-09-2008, 08:41 PM
  2. Replies: 2
    Last Post: 02-28-2008, 11:51 PM
  3. When to say when, huh?
    By correlcj in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-17-2002, 09:19 PM
  4. Huh? Can anyone explain?
    By Tarls in forum C++ Programming
    Replies: 11
    Last Post: 03-31-2002, 09:46 AM
  5. Huh? Why doesn't it work?
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-21-2002, 04:03 AM