none
How to use timer control in C#

Answers


  •     public partial class FormWithTimer : Form
        {
            Timer timer = new Timer();
            Label label = new Label();

            public FormWithTimer()
            {
                InitializeComponent();

                timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
                timer.Interval = (1000) * (1);              // Timer will tick evert second
                timer.Enabled = true;                       // Enable the timer
                timer.Start();                              // Start the timer

                label.Location = new Point(100, 100);
                label.AutoSize = true;
                label.Text = String.Empty;
               
                this.Controls.Add(label);
            }

            void timer_Tick(object sender, EventArgs e)
            {
                label.Text = DateTime.Now.ToString();
            }
        }
    Wednesday, May 02, 2007 9:26 AM

All replies


  •     public partial class FormWithTimer : Form
        {
            Timer timer = new Timer();

            public FormWithTimer()
            {
                InitializeComponent();

                timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
                timer.Interval = (1000) * (10);             // Timer will tick evert 10 seconds
                timer.Enabled = true;                       // Enable the timer
                timer.Start();                              // Start the timer
            }

            void timer_Tick(object sender, EventArgs e)
            {
                MessageBox.Show("Tick");                    // Alert the user
            }
        }
    Wednesday, May 02, 2007 8:50 AM
  • I managed before you said above but thanks Joshi. 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]
    [code]
     private void timer1_Tick(object sender, EventArgs e)
            {
                if (sender == timer1)
                {
                    DateTime.Now.ToLongDateString();
                }
            }
    [/code]

    How to show current system time on the Form?

    Wednesday, May 02, 2007 8:57 AM

  •     public partial class FormWithTimer : Form
        {
            Timer timer = new Timer();
            Label label = new Label();

            public FormWithTimer()
            {
                InitializeComponent();

                timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
                timer.Interval = (1000) * (1);              // Timer will tick evert second
                timer.Enabled = true;                       // Enable the timer
                timer.Start();                              // Start the timer

                label.Location = new Point(100, 100);
                label.AutoSize = true;
                label.Text = String.Empty;
               
                this.Controls.Add(label);
            }

            void timer_Tick(object sender, EventArgs e)
            {
                label.Text = DateTime.Now.ToString();
            }
        }
    Wednesday, May 02, 2007 9:26 AM
  • It worked.Thanks a million Joshi
    Wednesday, May 02, 2007 9:43 AM
  • thanks!
    • Proposed as answer by flower89 Saturday, November 21, 2009 9:30 AM
    Friday, September 12, 2008 4:01 AM
  • Good one. Just wondering, how can I use this with progress bar.

     

    Cheers,

    Venkatesan Prabu .J

    http://venkattechnicalblog.blogspot.com/


    Venkatesan Prabu .J http://venkattechnicalblog.blogspot.com/
    Thursday, July 01, 2010 7:20 AM
  • Timer timer = new Timer();
    public FormWithTimer()
            {
                InitializeComponent();

                timer.Tick += new EventHandler(timer_Tick); // it better tu do this by double Tab Key
                timer.Interval = 1;             
                timer.Enabled = true;
            }

            void timer_Tick(object sender, EventArgs e)
            {
               //Your code
            }
        }
    Saturday, August 21, 2010 7:34 AM
  • public partial class FormWithTimer : Form
        {
            Timer timer = new Timer();

            public FormWithTimer()
            {
                InitializeComponent();

                timer.Tick += new EventHandler(timer_Tick); // It better to do this whith double Tab Key
                timer.Interval = 1;             
                timer.Enabled = true;           
            }

            void timer_Tick(object sender, EventArgs e)
            {
                //Your Code
            }
        }
    Saturday, August 21, 2010 7:39 AM
  • Here is a complete source code with explanation

    How to use C# Timer >


    www.c-sharpcorner.com


    Monday, November 26, 2012 9:20 PM