I need help creating an elapsed timer with visual C++ 2008. Basically what I want to do is have elapsed time in seconds, minutes and hours tick down on a 'label' control. I've gotten it all set up, including the code for starting the timer ( this->tim_main->Enabled = true; )

All I need now is someone experienced with VC++ to help me out.

Thanks in advance.

The timer control allows you to specify a callback function that will get control whenever the timer fires. Usually when you double-click the control on the form, it will build the function stub for you.

Once in the callback, you can grab the contents of the label control, convert to an integer, subtract the value and put it back into the label control (converting it back to a string).

-Mike

Thanks mike,

Any chance of giving the key syntaxes and snippets to help me out? I'm kind of a noob at C++.

Thanks

I'll start from the top:

Open a new C++ "Windows Forms" project. On the main form, drag a Label Control and a Timer Control. Change the name of the label control (clicking on the label, then going to the property sheet) to "lblCount". Change the text property of the Label control to "30". Now, click on the Timer control. Again, using the property sheet, change the interval to 1000 (its in milliseconds), and ensure that the "Enabled" property is false.

Now, we have to mod the code. In "Form1.h", add the following line in the constructor for Form1:

this->timer1->Enabled = true;

This will start the timer, and it will fire every 1000 ms (1 sec). Now, go back to the form designer. Double-click on the Timer control. It will take you into the code into a newly-created member function named "timer1_Tick". Inside this function, add the following code:

int iCount = Convert::ToInt32(this->lblCount->Text);
iCount--;
if (iCount == 0)
{
[INDENT]this->lblCount->Text = "Done!";
this->timer1->Enabled = false;[/INDENT]
}
else
{
[INDENT]this->lblCount->Text = iCount.ToString();[/INDENT]
}

When run, the form will appear, countdown from 30, and when it hits zero, it will display "Done!" and turn off the timer.

Note there are obvious edit checks that are skipped, like putting a value other than "30" in for the label will cause the program to bomb.

Let me know if this works for you.

there was 1 error in the code:

error C2374: 'iCount' : redefinition; multiple initialization
(on the repeated line 'int iCount = Convert::ToInt32(this->lblCount->Text);'

I tried removing the line, and it counts down by two's.

However, If you remove the line at the top where it decrements (iCount--) it counts down properly. Thanks a lot, you were great help :)

Sorry, I just realised another problem. If the user were to enter text in three textboxes (Hours, Minutes and Seconds), and they were converted to int32's, how would it be programmed so that if the seconds reach 0, and there were still 13 minutes remaining, it would automatically restart from 59 seconds, taking away another minute?

Thanks in advance

Glad I could help (though I'm confused about the error; I pasted that code directly from my project..)

As far as 3 text boxes are concerned, you'll have to manage them yourself. If you change the example to use textboxes instead of labels (assuming the text boxes are named "txtHour", "txtMinute", "txtSecond"), the code in the timer_Tick routine would look something like:

int iHour = Convert::ToInt32(txtHour->Text);
int iMinute = Convert::ToInt32(txtMinute->Text);
int iSecond = Convert::ToInt32(txtSecond->Text);
iSecond--;
if (iSecond == -1)
{[INDENT]iSecond = 59;
iMinute--;
if (iMinute == -1)
{[INDENT]iMinute = 59;
iHour--;
if (iHour == -1)
{[INDENT]// Ending condition - turn off the timer
iHour = 0;
iMinute = 0;
iSecond = 0;
timer1->Enabled = false;[/INDENT]
}[/INDENT]
}[/INDENT]}
txtHour->Text = iHour.ToString();
txtMinute->Text = iMinute.ToString();
txtSecond->Text = iSecond.ToString();

Note that once you start letting users input the values, you will have to edit/validate them to ensure they don't enter trash characters - in the above case, the "ToInt32" functions will dump if you hand them trash. Also, you'll want to make sure they enter valid values, such as limiting the minutes and seconds to a range of 0-59.

Let me know if you have any more questions.

commented: Helped extremely well with making a timer. Explained code, gave understandable snippets. +1

You know, ur really quite amazing :) It works perfectly (lol yeah, i know, I'm retarded for not thinking of decrementing by second :P). And yes, I programmed it so if they enter hours higher than 24, A messageBox says, "You cannot enter hours higher than 24" and if you enter more than 59 mins, or 59 secs, etc.

Anyway, I added to ur reputation so thanks a ton.

Thanks - glad I could help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.