Search code examples
c#.netwinformswindowstate

A form that was changed from .Hide() to .Show() can't maximize,why?


I written a method that Hide or Show all forms of application(including forms children). The code is the following:

public enum FormState
{
    Show ,
    Hidden,
    Enable,
    Disable
}

private void SetAllFormsState(FormState formState)
{
    FormCollection forms = Application.OpenForms;
    FormWindowState formWinState;
    bool state;

    if (formState == FormState.Show)
    {
        formWinState = FormWindowState.Normal;
        state = true;
    }
    else if (formState == FormState.Hidden)
    {
        formWinState = FormWindowState.Minimized;
        state = false;
    }
    else
    {
        throw new ArgumentNullException("invalid flag");
    }

    for (int i = forms.Count - 1; i >= 0; i--)
    {
        Form form = forms[i];
        form.WindowState = formWinState;

        if (state)
        {
            form.Show();
        }
        else
        {
            form.Hide();
        }
    }
}

but when I call again the form:

SetAllFormsState(FormState.Show);

only the parent form can selected. The children forms is displayed in window/taskbar but seems "locked" by windows, can't be maximized or change to normal style. how I fix it?


Solution

  • Try setting the form.WindowState after the Show() and Hide() method calls:

    for (int i = forms.Count - 1; i >= 0; i--)
    {
      Form form = forms[i];
      if (state)
      {
        form.Show();
      }
      else
      {
        form.Hide();
      }
      form.WindowState = formWinState;
    }