Search code examples
c#.netwinformswindowstate

.Net: maintain form windowState when navigate to different forms


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


Solution

  • 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.