How to use timer control in C#
-
Wednesday, May 02, 2007 8:32 AM
How to use timer control in C#?
Any examples? Please Advise
All Replies
-
Wednesday, May 02, 2007 8:50 AM
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:57 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 9:26 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:43 AMIt worked.Thanks a million Joshi
-
Friday, September 12, 2008 4:01 AM
thanks!- Proposed As Answer by flower89 Saturday, November 21, 2009 9:30 AM
-
Thursday, July 01, 2010 7:20 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/ -
Saturday, August 21, 2010 7:34 AMTimer 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:39 AMpublic 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
}
} -
Monday, November 26, 2012 9:20 PM
- Edited by Mahesh ChandMVP Monday, November 26, 2012 9:21 PM


