Window WindowState to Maximized causes window to appear too early

I read that the Load event should be fired after the window handle was created, but before the window really became visible. For the most part this seems true. However, I found that when I create a form with the WindowState property set to FormWindowState.Maximized (either through the VS constructor or programmatically in the constructor), the window becomes visible before the Load event Load . For example:

 using System; using System.Windows.Forms; namespace MyApplication { public partial class MyForm : Form { public MyForm() { InitializeComponent(); WindowState = FormWindowState.Maximized; } protected override void OnLoad(EventArgs e) { MessageBox.Show("OnLoad - notice that the window is already visible"); base.OnLoad(e); } } } 

This, in turn, causes the displayed form to flicker a lot, while its controls (which are laid out during the Form.Load event) change when the window is visible. If I did not set the maximum state, then all the resizing will be done before the window is shown (as I expected).

I could hold on to setting WindowState until the end of the Load event, but it still causes a lot of flickering because the window becomes visible and then all the controls change.

Any thoughts?

+7
source share
4 answers

Try to delay the change in WindowState until the first time the activated event fires. This works for me on VB.NET with VS2005 and framework 2.0.

+2
source

If you need to put any diagnostic message in case of loading, use System.Diagnostics.Debug.WriteLine ();
If you use MessageBox, you will destroy the normal order of event events.

 protected override void OnLoad(EventArgs e) { System.Diagnostics.Debug.WriteLine("onLoad"); base.OnLoad(e); } 

This post explains in more detail.

+1
source

You must set WindowState BEFORE InitializeComponent ():

  public Form() //Constructor { WindowState = FormWindowState.Maximized; InitializeComponent(); } 
+1
source

Things that change the appearance of the window (for example, resizing) make the window become visible.

You can call .Hide() or .Visible = False in your ctor and make it visible again at the end of .Load

0
source

All Articles