How to use timer control in C#
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to use timer control in C#

  1. #1
    Join Date
    Mar 2007
    Posts
    149

    How to use timer control in C#

    Hi,

    How to use timer control in C#?

    Any example please? Thanks

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to use timer control in C#

    Hi,

    Like this:
    Code:
    //In your class
    Timer timer1;
    
    // Instantiate the timer
    timer1 = new Timer();
    
    // Setup timer
    timer1.Interval = 1000; //1000ms = 1sec
    timer1.Tick += new EventHandler(Timer_Tick);
    timer1.Start();
    
    //Event handler, will be called every 1 sec
    public void Timer_Tick( object sender, EventArgs eArgs )
    {
         if( sender == timer1 )
         {
              //Do something cool here
         }
    }
    Regards,

    Laitinen
    Last edited by laitinen; May 2nd, 2007 at 03:44 AM.

  3. #3
    Join Date
    Mar 2007
    Posts
    149

    Re: How to use timer control in C#

    I managed before you said above but thanks Shabbir. I did above now how to show current system time on the form at some specific position. Following is some sample of code I have
    Code:
    public Form1()
            {
                InitializeComponent();
    
                // Instantiate the timer
                timer1 = new Timer();
    
                // Setup timer
                timer1.Interval = 1000; //1000ms = 1sec
                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Start();
              
            }
    Code:
     private void timer1_Tick(object sender, EventArgs e)
            {
                if (sender == timer1)
                {
                    DateTime.Now.ToLongDateString();
                }
            }
    How to show current System time on the Form now?

  4. #4
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to use timer control in C#

    Quote Originally Posted by shah123
    I managed before you said above but thanks Shabbir.
    thanks who??

    Quote Originally Posted by shah123
    How to show current System time on the Form now?
    Depends where you want to show it?

    If you have a textbox on your form:
    Code:
    String time = DateTime.Now.ToLongDateString();
    textBox1.Text = time;
    Better approach would maybe be to put it on the statusbar. Add a statusStrip control to your form. From that add a toolStripStatusLabel and assign the string to the text property of this:
    Code:
    toolStripStatusLabel1.Text = time;
    Btw, it seems as an enormous overkill to fire the timer every 1 sec, if all you want to do is to update a "DateTime.Now.ToLongDateString". Then you have to wait really long to see any changes Try to swap it with a "ToLongTimeString". Just my $0.02!

    Laitinen
    Last edited by laitinen; May 2nd, 2007 at 04:05 AM.

  5. #5
    Join Date
    Mar 2007
    Posts
    149

    Re: How to use timer control in C#

    Sorry for the name Laitinen. Thanks a million for nice ideas but one more query is that this time is static not dynamic how to implement the time running?

  6. #6
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to use timer control in C#

    Quote Originally Posted by shah123
    Sorry for the name Laitinen. Thanks a million for nice ideas but one more query is that this time is static not dynamic how to implement the time running?
    No problem and you are welcome!

    Not sure I understand your question, but the time will update every second. If you decide to use ToLongTimeString you will see that the time updates every second. Maybe you can set the interval to 500ms to be sure to have it updated often enough.

    Laitinen

  7. #7
    Join Date
    Mar 2007
    Posts
    149

    Re: How to use timer control in C#

    Hey,

    Code:
     String time = DateTime.Now.ToLongTimeString();
      timeTextbox.Text = time;
    1. If you use above code as i did, how would you set the interval?
    2. One more query is that basically I wanted to use timer control to learn it, if you know any thing by that way let me know.

    Thanks,
    Regards,

  8. #8
    Join Date
    Mar 2007
    Posts
    149

    Re: How to use timer control in C#

    It worked by using following code. Thanks a million
    Code:
    timer1 = new Timer();
                timer1.Tick += new EventHandler(timer1_Tick);
                
                timer1.Interval = 1000;
                timer1.Start();
    void timer1_Tick(object sender, EventArgs e)
            {
                throw new Exception("The method or operation is not implemented.");
                timeTextbox.Text = DateTime.Now.ToString();
            }

  9. #9
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to use timer control in C#

    Quote Originally Posted by shah123
    1. If you use above code as i did, how would you set the interval?
    Code:
    timer1.Interval = 500 //Or whatever you want.
    Quote Originally Posted by shah123
    2. One more query is that basically I wanted to use timer control to learn it, if you know any thing by that way let me know.
    I think that what you just did is a good way of starting to learn the timer control. It shows the basics!

    Laitinen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center