Link to home
Create AccountLog in
Avatar of Éric Moreau
Éric MoreauFlag for Canada

asked on

Restoring WindowState to Normal from Minimized

Hi

One of my user reported a strange issue for a C# Windows Forms MDI application.

When the application closes, I save the WindowState, and the Bouds or RestoreBounds when the form is either Maximized or Minimized.

When the application restarts, I rehydrate the forms exactly like they were when the application closed.

My issue is that if a form is Minimized, I set its bound and then set the WindowState to Minimized. It is working great until there. But when the user tries to restore to the previous size, the window is correctly set to the previous top/left but width and height are not following correctly.

The strange thing is that the behavior works properly with the Maximized state!

Any ideas on how to ensure a from can be restored correctly?

Here is some code from my OnLoad override (Bounds are properly persisted):
            if (cSettings.GetProperty(FormSpecialName + ".WindowState").Trim().ToUpper() == "MAXIMIZED")
            {
                WindowState = FormWindowState.Normal;
                Bounds = cSettings.GetProperty(FormSpecialName + ".Bounds", new Rectangle(0, 0, 100, 100));
                WindowState = FormWindowState.Maximized;
            }
            else if (cSettings.GetProperty(FormSpecialName + ".WindowState").Trim().ToUpper() == "MINIMIZED")
            {
                WindowState=FormWindowState.Normal;
                Bounds = cSettings.GetProperty(FormSpecialName + ".Bounds", new Rectangle(0, 0, 100, 100));
                WindowState = FormWindowState.Minimized;
            }

Open in new window

Avatar of Russ Suter
Russ Suter

I've encountered this problem before. The actual dimensions of the window are not guaranteed to be correctly reported if the window is in a maximized or minimized state. You should introduce logic that does not store the bounds if the windowstate is other than Normal.
Avatar of Éric Moreau

ASKER

This is exactly why I use RestoreBounds!
... which apparently isn't working for you. As I said, I've encountered this problem before. The solution that worked for me was to only store bounds when the window state was normal. Perhaps you could post your complete sample. I'd be happy to work with you to find the answer.
I told in my question that the values are correctly persisted.
Fair enough. I'm interested in helping, not arguing. Good luck!
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Found my own solution.