Avatar of Éric Moreau
Éric Moreau
Flag 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

C#Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Éric Moreau

8/22/2022 - Mon
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.
Éric Moreau

ASKER
This is exactly why I use RestoreBounds!
Russ Suter

... 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 started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Éric Moreau

ASKER
I told in my question that the values are correctly persisted.
Russ Suter

Fair enough. I'm interested in helping, not arguing. Good luck!
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Éric Moreau

ASKER
Found my own solution.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.