mcriscolo 47 Posting Whiz in Training

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.

Wiki_Tiki commented: Helped extremely well with making a timer. Explained code, gave understandable snippets. +1
mcriscolo 47 Posting Whiz in Training

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.

mcriscolo 47 Posting Whiz in Training

Jennifer,

OK, here's a complete rundown:

I'm assuming you are working with a "Windows Forms" app. If you call the form that you get when you start the project "Form1", this code will show you how to open a new form, and close it by pressing a button on Form1.

First, add another Windows form to the project; call it "Form2".

In that form, add a function, called FormClose10:

public:
void FormClose10()
{
[INDENT]this->Close(); // CoolGamer48 is correct, you can just say "Close()" if you want - I'm wordy...[/INDENT]
}

In the Form1 designer, add 2 buttons, one labeled "Open" and another labeled "Close".

In the Form1.h file, put the following code:

#include "Form2.h"

right after the "#pragma once" at the top of the file.

In Form1.h, inside the class definition, add the following code:

private:
[INDENT]Form2 ^myForm2; // this is different than what I first told you -- sorry

[/INDENT]

In the "Open_Click" routine, add the following code:

myForm2 = gcnew Form2();
myForm2->Show();

In the "Close_Click" routine, add the following code:

myForm2->FormClose10();

That should do it. When you run the program, Form1 will come up with 2 buttons; press "Open" and Form2 should appear... press "Close" on Form1 and Form2 should disappear.

If you have any problems, let me know. I can send a C++ project to you that has this code working if you like.

mcriscolo 47 Posting Whiz in Training

Jennifer,

OK, a few things:

In Form1, if you don't want to, you don't have to pass "sender" and "e" to the public function. So the function declaration in the form can just be:

public void FormClose10();

In Form2, you will need an instance of Form1 (I'm paraphrasing from your first post, where you declared a pointer to the form), something like this:

In Form2:

Form1 *myForm1 = new Form1();
...
<do stuff with the Form1>
...
myForm1->FormClose10();

The key is that you have to have a reference variable (in this case "myForm1") in order to call the public function.

In this example:

Form1::FormClose10();

This is trying to call a static version of the function, which is not what you want. The form has to be instantiated, which is what the call:

Form1 myForm1 = new Form1();

does. Then you can monkey with it.

Let me know if I can be of more help.

mcriscolo 47 Posting Whiz in Training

Jennifer,

I believe what you want is a public function on Form2's class. For example, you can make a public function called "CloseForm" on Form2. Inside "CloseForm" you would call the close function:

this->Close();

Then, from Form1, when your button is pressed, you would issue the call:

form2->CloseForm();

Hope this helps!

mcriscolo 47 Posting Whiz in Training

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