CreganTur
11-19-2008, 12:54 PM
There were some discussions a while ago about the possability of adding a C/C++ section to the forum... I don't know if that will happen, but I figured I'd go ahead and offer a seed post:)
I'm just starting to learn Visual C++ by working through the book Microsoft Visual C++ Express for the Absolute Beginner (http://www.amazon.com/Microsoft-Express-Programming-Absolute-Beginner/dp/159200816X/ref=sr_1_8?ie=UTF8&s=books&qid=1227123985&sr=1-8). Good book so far, only gone through the first 2 chapters but I've learned a lot about the IDE.
Anyway, I'm building an improved version of Chapter 2's project (every chapter ends with some challenges to work out on your own). I've got a textbox on my Form that displays a seconds count. Currently it counts from 1 to 30 seconds. It is fed by a Timer object, which is set with an interval of 1000 miliseconds.
What I want to do is flip this around into a countdown timer, so it runs from 30 to 0. The timer's counter intervals are held in the variable intTimerCounter (code to follow). If this was VBA I would just set the text of the textbox to be "30 - intTimerCounter", but that doesn't work in C++; it gave me an error of some sort...
Here's my current code:
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
//update the timer
intTimerCounter++;
textBox2->Text = intTimerCounter.ToString();
if( intTimerCounter == 30 )
{
//if the timer reaches 30 disable the buttons
button1->Enabled = false;
button2->Enabled = false;
timer1->Enabled = false;
}
}
The code above is for the Timer object. It increments the intTimerCounter variable until it reaches 30, at which point it disables 2 buttons on the Form and then disables the timer. It also changes the Text of textBox2 to show the current value of intTimerCounter.
Anyone have any ideas on what I can do to change this to a countdown timer?
I'm just starting to learn Visual C++ by working through the book Microsoft Visual C++ Express for the Absolute Beginner (http://www.amazon.com/Microsoft-Express-Programming-Absolute-Beginner/dp/159200816X/ref=sr_1_8?ie=UTF8&s=books&qid=1227123985&sr=1-8). Good book so far, only gone through the first 2 chapters but I've learned a lot about the IDE.
Anyway, I'm building an improved version of Chapter 2's project (every chapter ends with some challenges to work out on your own). I've got a textbox on my Form that displays a seconds count. Currently it counts from 1 to 30 seconds. It is fed by a Timer object, which is set with an interval of 1000 miliseconds.
What I want to do is flip this around into a countdown timer, so it runs from 30 to 0. The timer's counter intervals are held in the variable intTimerCounter (code to follow). If this was VBA I would just set the text of the textbox to be "30 - intTimerCounter", but that doesn't work in C++; it gave me an error of some sort...
Here's my current code:
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
//update the timer
intTimerCounter++;
textBox2->Text = intTimerCounter.ToString();
if( intTimerCounter == 30 )
{
//if the timer reaches 30 disable the buttons
button1->Enabled = false;
button2->Enabled = false;
timer1->Enabled = false;
}
}
The code above is for the Timer object. It increments the intTimerCounter variable until it reaches 30, at which point it disables 2 buttons on the Form and then disables the timer. It also changes the Text of textBox2 to show the current value of intTimerCounter.
Anyone have any ideas on what I can do to change this to a countdown timer?