I'm going to answer your question very "literally," assuming that you want to start a WinForms application with its "Primary Form" hidden, and an InitialForm displayed where the user enters a name, and then, when the user closes the InitialForm, you want your PrimaryForm (the Application) to become visible, and to have "captured" the information entered on the InitialForm, so you can use it in your Application (the PrimaryForm).
So, the InitialForm code is something like this: assuming you have a TextBox, and a Button, on it, and the Button Click Event wired-up:
public partial class InitialForm : Form
{
public InitialForm() { InitializeComponent(); }
public string UserEnteredText { get; private set; }
private void button1_Click(object sender, EventArgs e)
{
UserEnteredText = textBox1.Text;
Close();
}
}
So, the PrimaryForm (that you start your Application with) contains:
private void PrimaryForm_Shown(object sender, EventArgs e)
{
Visible = false;
InitialForm iform = new InitialForm();
iform.FormClosing += iform_FormClosing;
iform.Show();
}
private string UserEnteredText;
private void iform_FormClosing(object sender, FormClosingEventArgs e)
{
UserEnteredText = (sender as InitialForm).UserEnteredText;
Show();
MessageBox.Show("The user entered: " + UserEnteredText);
}
But, wait: what happened if the InitialForm had a CloseBox, and the user closed it ? The public string property of the InitialForm
would never have been set: so you need to add a FormClosing EventHandler to the InitialForm, and "wire it up" (as shown in the commented out code above for InitialForm):
private void InitialForm_FormClosing(object sender, FormClosingEventArgs e)
{
UserEnteredText = textBox1.Text;
}
Consider the upside, and downside, of using the technique shown here:
1. upside: InitialForm is disposed of: you never "kept a reference to it." You didn't have to write a custom Event in the InitialForm, and subscribe to it in the Primary Form. You didn't need (praise be) to muck around with WinForm's tricky Property Change notification. You didn't need to do anything "modal."
2. downside: You don't keep InitialForm around for possible re-use: and that's likely (?). Writing a custom Event in InitialForm, triggered by the Button Click Event, that you subscribed to in the PrimaryForm, might well result in "cleaner," more understandable, maintainable code.
Finally: by simply making InitialForm have no CloseBox, you can eliminate the fancy-dancing shown here the handle the case of the user closing InitialForm in a way that prevents you from "getting" the information from its TextBox.