2
votes

In my application, user can navigate to one form to other and so on. So i want to maintain the WindowState of each form. My mean is that if form1 is in Normal mode then next form2 should be open in Normal mode and if form1 is in maximize mode then form2 should be open in maximize mode. Same approach is applicable when user navigate to previous form. I am not using MDI Parent - child.

One way of doing this is as following

frmLogin form = new frmLogin();
form.WindowState = this.WindowState;
form.Show();
this.Hide();

but I think this approach is not good. in this case i must maintain previous form reference to new form so when new form close then previous form occupy the new form windowState

Please suggest a better alternative

3

3 Answers

2
votes

You could keep a global variable (a static on some class) with the current WindowState. When the window state of a form changes, you set that variable, and when you create a new form (e.g. in the constructor), you read the window state from that variable.

Note that there is no WindowStateChanged event. However, changing the window state triggers the Resize event, so you can use that.

You could even put all of this in a base class:

public class FormBase : System.Windows.Forms.Form
{
    private static FormWindowState _windowState = FormWindowState.Normal;

    public FormBase()
    {
        WindowState = _windowState;
    }

    protected override void OnResize(EventArgs e)
    {
        _windowState = WindowState;

        base.OnResize(e);
    }
}

And if you want to go really fancy, you can synchronize the form state of all forms:

public class FormBase : System.Windows.Forms.Form
{
    private static List<FormBase> _allForms = new List<FormBase>();
    private static FormWindowState _windowState = FormWindowState.Normal;

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        _allForms.Add(this);

        WindowState = _windowState;
    }

    protected override void Dispose(bool disposing)
    {
        _allForms.Remove(this);

        base.Dispose(disposing);
    }

    protected override void OnResize(EventArgs e)
    {
        _windowState = WindowState;

        foreach (var form in _allForms)
        {
            if (form != this)
            {
                form.WindowState = WindowState;
            }
        }

        base.OnResize(e);
    }
}
2
votes

Have a central place (for example, an object of a FormStateManager class) where you store the WindowState information, available for every of your Forms. You can use either singleton pattern for having just one FormStateManager object in your program, or pass the right object in every Form's constructor. The FormStateManager object has to be informed whenever one form changes it's WindowState, for example, in the Resize event.

0
votes

Don't see any problem in approach folowed by you. You have FormWindowState type one variable that holds last "valid" (from point of view of your app) state for all forms.