1
votes

I have 5 forms as following:

  • Form1
  • Form2
  • Form3
  • Form4
  • Form5

Form1 is my startup form.i have 4 button in this form to show other forms(form2,form3,form4,form5).I want show other forms behind of the form1, then when i minimize form1 ,other forms minimized too and when i restore form1 other forms restored too.However form1 always is top of other forms.

How can i implement this?

3

3 Answers

3
votes

Try this in your top form.

private List<Form> subForms;
private bool minimized = false;

public TopForm()
{
    InitializeComponent();
    subForms = new List<Form>();
    subForms.Add(new SubForm(1));
    subForms.Add(new SubForm(2));
    subForms.Add(new SubForm(3));
    subForms.Add(new SubForm(4));
    subForms.Add(new SubForm(5));

    foreach (Form f in subForms)
    {
        f.Show();
    }

    BringToFront();
    Resize += OnResize;
}

/// <summary>
/// Detects a resize event and handles it according to window state.
/// </summary>
/// <param name="sender">Top form</param>
/// <param name="args">Unused</param>
private void OnResize(object sender, EventArgs args)
{
    switch (WindowState)
    {
    case FormWindowState.Normal:
        if (minimized)
        {
            minimized = false;
            OnRestore();
        }
        break;
    case FormWindowState.Minimized:
        minimized = true;
        OnMinimize();
        break;
    }
}

/// <summary>
/// Minimize all sub forms
/// </summary>
public void OnMinimize()
{
    foreach (Form form in subForms)
    {
        form.WindowState = FormWindowState.Minimized;
    }
}

/// <summary>
/// Restore all forms and bring them to the front, with this main form on top.
/// </summary>
public void OnRestore()
{
    foreach (Form form in subForms)
    {
        form.WindowState = FormWindowState.Normal;
        form.BringToFront();
    }
    BringToFront();
}
0
votes

Use composition inheritance to delegate the tasks of your Z-order forms into the Form1 class, or create an intermediary class for those forms that has access to the variables of all those forms. Then:

To set topmost:

<Form>.TopMost = True;

See: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost(v=vs.71).aspx

To have the forms restore and minimize in a particular order (this would be in your controlling class) Restore them in reverse order, meaning you show form 4 then form 3 then form 2 then form 1, then set the topmost property of form 1.

Then, use the minimized handlers in each of the forms and integrate that with your controller code, so that when anytime Form1 is minimized, everything else minimizes (and vice versa)

Good Luck!

0
votes

Does it really make sense for children forms to appear behind a main form? What if the parent form is fully maximized?

Otherwise the button click for each form would look something like this:

private void subFormBtn_Click(object sender, EventArgs e)
        {
            SubForm subForm = new SubForm();
            subForm.Show(this);

        }

This sets the parent of the sub-form to the form that contains the button click.

If you really need the sub-forms to appear behind the main form you need to do something like Benjamin Danger Johnson suggested.