Re: Odd behavior with WindowState by Jerad
Jerad
Tue Dec 11 07:55:19 PST 2007
Thanks for the responses, everyone.
To respond to a few of your points...
From AMercer:
> I am in the habit of setting visible=false at the top of form_load, and
> visible=true at the bottom. Somewhere between, I set window if necessary.
> I have never had a problem like you describe, no flicker, nothing. I am
> suspicious of your not setting visible to true in the sample above.
If I did this, I would also see no flicker. But then my problem would be
the controls not rendering properly because of the bounds not being set on
the form properly. For example, put this code on a blank form:
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
Console.WriteLine(this.Bounds.ToString());
this.WindowState = FormWindowState.Maximized;
Console.WriteLine(this.Bounds.ToString());
this.Visible = true;
Console.WriteLine(this.Bounds.ToString());
}
And you will see that the second this.Bounds is still as it was before it
was maximized. If, prior to making your form visible, you make various
changes to the positioning of your controls, it is relative to the normal
window's bounds, rather than the maximized window's bounds.
> Where is it made visible?
It is shown naturally after the Load event. If, using the above example, I
were to leave off "this.Visible = true;", it would still show, but only show
a normal window.
> You could use form.MaximizedBounds for the size of the form when it is
> maximized. From your above remarks, it sounds like you are working around
> the fact that setting window in form_load does not immediately change
> form.width (for example). Better, I think, to use MaximizedBounds and do
> all this work in the load event.
This is actually a good suggestion -- I was not aware of this property.
However, I'm not using Bounds to position my controls -- the .NET framework
is. Specifically, I have SplitContainers whose position I am trying to set
before the form is shown, but it is showing these based off the form's
Bounds, which is incorrect until it is made visible. That's my problem.
From Michael C:
> The problem is in your code. It's not a bug or oversight, you're just not
> doing what the designers expect.
While I won't argue that there are better ways to do what I'm doing, that
still doesn't explain:
1) Why setting the WindowState causes the form to show permaturely (when not
explicitly setting Visible = false).
2) Why setting the WindowState with the form explicily hidden (Visible =
false) does not update the window's Bounds to reflect the Maximized state.
It seems there is no way for .NET framework to recognize that a window has
been maximized without having to show it. This, to me, seems to be either
an oversight or a bug (or limitation due to OS) -- I can't make sense out of
why this would have been intentional (by design). I recognize that I could
be wrong though, so please clarify if I'm still missing something.
> Yes but that is certainly not it. Why not just set the WindowState
> property to Maximized in the properties window? Otherwise I think you can
> set it in the forms constructor.
Specifically, I am saving and loading the user's settings for the form from
the last time it was loaded and closed. I want it to open as maximized if
they closed it as maximized, and as normal if they closed it as normal.
I did try the same code above in the constructor rather than on Load, but
with the same results -- it would not recognize that the form was maximized
until it was shown, and therefore the Bounds was not set properly. The only
difference is that if I don't explicitly set Visible to false/true, it will
leave the form hidden on setting WindowState rather than showing it. But in
this case, the resulting output would always reflect the bounds of the
normal form window -- it wouldn't recogniz it as being maximized at all
during the constructor code.
Thanks again for the responses, but I'm still stuck. The best workaround
I've been able to come up with so far, is set as many settings as possible
prior to showing the form, and then show the form so that I can set my
SplitContainer's SplitterDistances properly.
Jerad