Creating "owned" Forms is a good way to implement secondary windows that will, when they are visible, always stay in front of your Main Form(s). Examples of use of owned windows are search-and-replace dialog boxes, color-pickers, etc.
Here's a "design model" I've found useful:
1. Create an instance of the secondary Form in your Main Form, set its 'Owner Property, and wire-up a 'FormClosing EvenHandler to it:
private SecondaryForm secForm;
private void MainForm_Load(object sender, EventArgs e)
{
secForm = new SecondaryForm();
secForm.Owner = this;
secForm.FormClosing += secForm_FormClosing;
this.FormClosing += secForm_FormClosing;
}
private void secForm_FormClosing(object sender, FormClosingEventArgs e)
{
secForm.Hide();
e.Cancel = true;
}
At this point, you encounter an interesting side-effect of the Owner/Owned relationship in WinForms: if you try and close the Main Form, it will not close, because: it cannot close the Secondary Form !
2. The fix for this problem is to define a 'FormClosing Event for the Main Form:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
secForm.FormClosing -= secForm_FormClosing;
secForm.Close();
e.Cancel = false;
}
In this EventHandler, we remove the 'FormClosing EventHandler from the instance of the SecondaryForm, and close it, and then the Application can close in the usual way.
What's (obviously) missing from the above example is the code to make the secondary Form visible ('Show, or set 'Visible Property to 'true). You may wish to implement
that in various ways depending on your Application.