0

Although I'm using C# .NET 6.0 this will probably relate to other frameworks also.

Take a Windows Form App with a resizable form that can be minimized and maximized. The user minimizes the form after they have maximized it. When the user restores the window from the taskbar the form will return to maximized. However, if I need to unminimize the form programmatically I need to use the WindowState property, but if I use Normal it will change the form back to regular size if I use Maximized it'll change it to maximized if the form was previously normal. How do I easily determine if the minimized form was previously in a maximized state? I am currently doing this by using the Resize event and reading WindowState in that event to record the state of the window. But it's rather a messy solution, is there a less greasy way to do this?

    bool isMaximized = false;
    private void Form_Resize(object? sender, EventArgs e)
    {
        switch (WindowState)
        {
            case FormWindowState.Normal:
                isMaximized = false;
                break;
            case FormWindowState.Maximized:
                isMaximized = true;
                break;
        }
    }

    private void ShowForm()
    {
        if (WindowState == FormWindowState.Minimized)
        {
            WindowState = isMaximized ? FormWindowState.Maximized : FormWindowState.Normal;
        }
    }
  • Does this answer your question? [Using C# FormWindowState to restore up?](https://stackoverflow.com/questions/2043990/using-c-sharp-formwindowstate-to-restore-up) – Poul Bak Feb 19 '22 at 18:19
  • The suggested similar question here: https://stackoverflow.com/questions/2043990/using-c-sharp-formwindowstate-to-restore-up has solutions which use the same method I am, or require the importing of user32.dll which is still not ideal. What I was looking for is a neat, less bloaty way to do it but I don't think there's a tidy solution to this. – Marcus Dudley Feb 19 '22 at 18:24

1 Answers1

0

I think no, but did want to point out the code you have seems verbose compared to storing a FormWindowState:

private FormWindowState _prev;
private void Form_Resize(object? sender, EventArgs e)
{
    if(WindowState != FormWindowState.Minimized) _prev = WindowState;
}

private void Restore()
{
    WindowState = _prev;
}
Caius Jard
  • 72,509
  • 5
  • 49
  • 80