Hi,
I have a placed a button control and a textbox on a form. I have also placed a timer control on form. When the form is first loaded the textbox is hidden. When the button is clicked the textbox is made visible. This is coded into the button’s click event. If nothing is entered into the textbox within, say, 10 seconds I would like the form to return to its original state with the textbox hidden, timer control set to zero and the button waiting to be clicked. How might I do that? Is there a property that will return the form to its original state, that is, when the form was first loaded. Incidentally, how would I dispose of a form? I would be grateful for all help.
AlanPosted Apr 8, 2008, 2:23 PM
There's no property which can do this but, in this particular case, its easy to do it in code.
Suppose the controls are called textBox1, button1 and timer1.
Begin by setting the textBox1's Visible property to false in the designer.
Also set timer1's Interval property to 10000 and Enabled property to false.
Now add these two eventhandlers, build and run:
private
void button1_Click(object sender, EventArgs e){
if (!textBox1.Visible){
textBox1.Visible =
true;textBox1.Focus();
timer1.Start();
}
}
private void timer1_Tick(object sender, EventArgs e){
if (textBox1.Text == ""){
textBox1.Visible =
false;}
timer1.Stop();
}
To dispose of a form call its Close() method or, in the case of a modal or MDI form, its Dispose() method. Provided there are no references to it in the application, then the GC will dispose of it in due course. Notice that if you close the startup form, this will cause the entire application to terminate.