Windows Develop Bookmark and Share   
 index > Windows Forms General > Display Timer
 

Display Timer

Hi all.

I'm new to programming and C# is my first language.

I'm currently stuck at a problem of displaying a timer in the windows form that i've designed.
I've created a label on the form on which i want to display a timer which keeps counting the number of seconds and minutes till the time the form is not closed.

Plz help.
Thanks in advance..
jay_1648  Wednesday, October 24, 2007 5:41 PM

Create a timer and set its Interval to 1000 .This code will count how many seconds have elapsed since

the application started.

Code Block

public partial class Form1 : Form

{

private TimeSpan timecounter = new TimeSpan();

private void Form1_Load(object sender, EventArgs e)

{

timer1.Enabled = true;

timer1.Interval = 1000;

}

private void timer1_Tick(object sender, EventArgs e)

{

timecounter = timecounter.Add(TimeSpan.FromSeconds(1));

label2.Text = timecounter.TotalSeconds.ToString() + " s";

}

}

Fábio Franco  Wednesday, October 24, 2007 6:57 PM

Hi

Put a Label and a Timer onto your Form.

Use Form event FormLoad and Timer event Tick.

Code Block

private void Form1_Load(object sender, EventArgs e)

{

timer1.Enabled = true;

timer1.Interval = 100;

}

private void timer1_Tick(object sender, EventArgs e)

{

label1.Text = DateTime.Now.ToString("HH:mm:ss");

}

Maybe you should start with a beginner C# Book. There are many free books to find in web.

Regards

Flo

FlorianReischl  Wednesday, October 24, 2007 5:57 PM

Create a timer and set its Interval to 1000 .This code will count how many seconds have elapsed since

the application started.

Code Block

public partial class Form1 : Form

{

private TimeSpan timecounter = new TimeSpan();

private void Form1_Load(object sender, EventArgs e)

{

timer1.Enabled = true;

timer1.Interval = 1000;

}

private void timer1_Tick(object sender, EventArgs e)

{

timecounter = timecounter.Add(TimeSpan.FromSeconds(1));

label2.Text = timecounter.TotalSeconds.ToString() + " s";

}

}

Fábio Franco  Wednesday, October 24, 2007 6:57 PM
I think this was the solution i was looking for.
Thanks Fabio for your help. I'm not at home right now to try this code but i'm sure this will work. As i was not familiar with the timespan class that's why it was a problem for me.
I did declare a timer class object but that wasn't giving any member variable which displays the current time in the timer. I did notice the tick event but didn't know how to use it. But I should be able to use it now. Thanks again for your help.

Just a silly question again what's this tick event though and when exactly it's called. Is it called on every second or in some milliseconds or when exactly it is called.. ?
jay_1648  Wednesday, October 24, 2007 7:25 PM

The Timer component is just for counting time. The tick event is called every time a specified interval has elapsed.

Example: You set the Interval for your Timer to 100. This means that every 100ms (One 10th of a second) the

Timer.Tick event will get called. This way you can make a counter or anything you like that needs to be done

every 100ms.

In the case I supplied, you set the interval to 1000 (1000ms or One second). That means that every second

the Timer1_Tick event handler will be called and in the event handler I add One Second to the TimeSpan object.

So timecounter will be one second longer at every 1000ms.

TimeSpan is pretty much a class that represents a period of time. As TimeDate represents Date/Time like

calendar.

Fábio Franco  Wednesday, October 24, 2007 7:35 PM

Hi,

The code you told me earlier did work and it's simple as well. But it goes on counting the seconds only. I would need to have the minutes also displayed along with the seconds. In every sixty second one minute should increase and second will start again.

I would really appreciate the help..

Thanks again .

jay_1648  Thursday, October 25, 2007 10:05 AM

I did put only the seconds on purpose, I think it is important to explore things yourself. Makes your mind get sharp. I think you should try to figure it out yourself, try it, explore. If you still can't get it to work, post here again and I will post the code for you.

Regards,

Fábio

Fábio Franco  Thursday, October 25, 2007 10:31 AM

Well I know i certainly can do it , just need to spend some time on it. I'm trying it now but I might end up with a complicated code. But whatever code i put i'll post here so that you can check if that's good or not. Thanks.

jay_1648  Thursday, October 25, 2007 10:47 AM

Apart from this timer Problem I'm also stuck at another problem also , which is to use a resource file.

I've created an additional resource file for my project coz i want to store some string values on it.

I have right clicked on solution explorer and clicked "add new item" and added a new resource file named as "Resource1.resx"

I can go to this file by manually double clicking it and i can manually add some string objects and values on it. But I want do it on runtime. Like I want to access it from my code. To retreive the string object and values in runtime i simply use

"Resource1.stringObject"

But how do i add string objects to it in runtime. ?

jay_1648  Thursday, October 25, 2007 10:59 AM

Here's my code to display minutes along with the seconds:

private void Form1_Load(object sender, EventArgs e)

{

timer2.Enabled = true;

timer2.Interval = 1000;

}

int sec = 0;

int min = 0;

private void timer2_Tick(object sender, EventArgs e)

{

sec++;

if (sec == 60)

{

sec = 0;

min++;

}

label1.Text = Convert.ToString(min) + ":" + Convert.ToString(sec);

}

jay_1648  Thursday, October 25, 2007 11:36 AM

I see you used a different approach here, not using timespan, which is good too. in the label part you should

do differently:

Code Block

label1.Text = min.ToString() + ":" + sec.ToString();

About the strings, do you actually want to create new strings or just modify them during run-time?

Fábio Franco  Thursday, October 25, 2007 11:44 AM

A better one to display time on the screen will be :

Code Block

private void Form1_Load(object sender, EventArgs e)

{

timer2.Enabled = true;

timer2.Interval = 1000;

}

private void timer2_Tick(object sender, EventArgs e)

{

sec++;

second = Convert.ToString(sec);

minute = Convert.ToString(min);

if (sec == 60)

{

sec = 0;

min++;

}

if (sec < 9)

{

second = "0"+Convert.ToString(sec);

}

if (min < 9)

{

minute = "0"+Convert.ToString(min);

}

label1.Text = minute + ":" + second;

}

jay_1648  Thursday, October 25, 2007 11:52 AM
jay_1648 wrote:

Apart from this timer Problem I'm also stuck at another problem also , which is to use a resource file.

I've created an additional resource file for my project coz i want to store some string values on it.

I have right clicked on solution explorer and clicked "add new item" and added a new resource file named as "Resource1.resx"

I can go to this file by manually double clicking it and i can manually add some string objects and values on it. But I want do it on runtime. Like I want to access it from my code. To retreive the string object and values in runtime i simply use

"Resource1.stringObject"

But how do i add string objects to it in runtime. ?

Can you help me with this as well ?

I really need to get it done as quickly as possible.

Thanks again.

jay_1648  Thursday, October 25, 2007 12:16 PM

Hi, Jay
I recommend you to start anew thread for a different question for better responses. Thanks for understanding,

Zhi-Xin Ye  Monday, October 29, 2007 7:28 AM

You can use google to search for other answers

Custom Search

More Threads

• Current Context Error
• vs2008 Menus
• Need suggestions and ideas for creating a "GadgetBase" and simplifying any coding if possible...
• selecting text in a text box
• Snap In definition files
• Application.DoEvents and repeating events
• Synchronize scrolling in 2 richTextBoxes in C#
• Scrollbar fires twice on every click?
• tooltip shows wrong message
• Winform design standards?