Hi,
How to use timer control in C#?
Any example please? Thanks
Printable View
Hi,
How to use timer control in C#?
Any example please? Thanks
Hi,
Like this:
Regards,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
}
}
Laitinen
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();
}
How to show current System time on the Form now?Code:private void timer1_Tick(object sender, EventArgs e)
{
if (sender == timer1)
{
DateTime.Now.ToLongDateString();
}
}
thanks who?? :)Quote:
Originally Posted by shah123
Depends where you want to show it?Quote:
Originally Posted by shah123
If you have a textbox on your form:
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:String time = DateTime.Now.ToLongDateString();
textBox1.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!Code:toolStripStatusLabel1.Text = time;
Laitinen
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!Quote:
Originally Posted by shah123
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
Hey,
1. If you use above code as i did, how would you set the interval?Code:String time = DateTime.Now.ToLongTimeString();
timeTextbox.Text = time;
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,
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();
}
Quote:
Originally Posted by shah123
Code:timer1.Interval = 500 //Or whatever you want.
I think that what you just did is a good way of starting to learn the timer control. It shows the basics!Quote:
Originally Posted by shah123
Laitinen