@tlhintoq
Good point - when you open a form using ShowDialog(), it is not actually closed (and therefore not disposed) when you try to close it. In fact, you should dispose it manually when you don't need it anymore, to make sure that all handles are released as soon as possible.
Adding custom events to modal dialog boxes for basic user input is rarely necessary (on the other hand, thinking twice before posting is often necessary - my fault) :).
For example, you could add a property to your dialog (Form2) in order to encapsulate your data a bit (your TextBox control should remain private):
- public string EnteredText
-
{
-
get { return textBox1.Text; }
-
}
-
And then access that property just before disposing the dialog. Common way to dispose a dialog immediately after use is using the "using pattern":
- private void button1_Click(object sender, EventArgs e)
-
{
-
using (Form2 f2 = new Form2())
-
{
-
if (f2.ShowDialog() == DialogResult.OK)
-
{
-
MessageBox.Show("Entered text was: " + f2.EnteredText);
-
}
-
}
-
}
The "using" construct not only makes a smaller scope for f2, but also makes sure that the form is closed and disposed after the construct.