| Author: Pavan 07 Nov 2009 | Member Level: Bronze | Rating:   Points: 3 |
Try This it may help you......
using System; using System.Windows.Forms; using System.Drawing;
class TimerDemo : Form { Timer Clock; Label lbTime = new Label(); private int hour=0, min = 2, sec = 30; public TimerDemo() { this.Controls.Add(lbTime);
Clock = new Timer(); Clock.Interval = 1000; Clock.Start(); Clock.Tick += new EventHandler(Timer_Tick); lbTime.BackColor = Color.Black; lbTime.ForeColor = Color.Red; lbTime.Font = new Font("Times New Roman", 15); lbTime.Text = GetTime(); }
public string GetTime() { string TimeInString = ""; Boolean finished = false; sec--; if (sec < 0) { min--; if (min < 0) { if (hour > 0) { hour--; min = 59; sec = 59; } else { finished = true; } } else { sec=59; } } if (finished) { TimeInString = "Time's UP";
Clock.Stop(); } else { TimeInString = (hour < 10) ? "0" + hour.ToString() : hour.ToString(); TimeInString += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString()); TimeInString += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString()); } return TimeInString; }
public void Timer_Tick(object sender, EventArgs eArgs) { if (sender == Clock) { lbTime.Text = GetTime(); } }
public static void Main() { Application.Run(new TimerDemo()); } }
|
| Author: Thillai NathaN 09 Nov 2009 | Member Level: Silver | Rating:  Points: 2 |
hi Naresh,
Based on the trick event of the timer the following process goes. The following code shows the remaining time in seconds. you can change it by doing math calculations so as to display the remaining time in minutes and seconds.
int i = 15000 // 1000ms = 1 second. public void time() { time1.Interval = 15000; time1.Enabled = true; time1.Start(); time1.Tick += new EventHandler(time1_Tick_1); } private void time1_Tick_1(object sender, EventArgs e) { if (i >0) { lbltime.Text = i.ToString(); i--; } }
|