8

I read that the Load event is supposed to be fired after the window handle has been created, but before the window actually become visible. For the most part, this seems to be true. However, I've found that when I create a form with the WindowState property set to FormWindowState.Maximized (either via the VS designer, or programatically in the constructor), the window becomes visible prior to the Load event firing. 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) are resized while the window is visible. If I did not set the state to be maximized, then all the resizing is done before the window is shown (which is what I would have expected).

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

Any thoughts?

g t
  • 7,287
  • 7
  • 50
  • 85
user1283610
  • 116
  • 1
  • 1
  • 4
  • 3
    That `MessageBox.Show` in the load event will force the form to show. – LarsTech Mar 21 '12 at 14:34
  • 1
    Resizing the controls in the `Load` event is somewhat suspicious. Why don't you rely on the built-in resizing mechanisms for controls, like `Dock` or `Anchor` properties or the `TableLayoutPanel`? Will you manually resize the controls as well when I resize the window manually later on? – Thorsten Dittmar Mar 21 '12 at 14:39
  • 1
    I'm getting this as well. I'm saving the WindowState between user sessions so that the form can be resized, etc to what the user previously had it and when I set the WindowState to Maximized I get a lot of flickering, etc. Is there a solution to fix this? – John Grabanski Nov 27 '17 at 18:52

4 Answers4

2

Try to delay the change of WindowState until the first Activated event firing. This works for me in VB.NET with VS2005 and framework 2.0.

jramos
  • 128
  • 1
  • 6
  • This worked for me when the windowstate was being changed to normal from maximized. Instead I set it to maximized in the window Load event. – NielW May 09 '14 at 16:37
2

You have to set WindowState BEFORE InitializeComponent():

    public Form() //Constructor
    {
        WindowState = FormWindowState.Maximized;

        InitializeComponent();
    }
Vinicius Gonçalves
  • 2,514
  • 1
  • 29
  • 54
1

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

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

This post explain more details

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Things that change the appearance of the window (resizing for instance) cause the window to become visible.

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

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • Setting visible to false does not seem to have any effect. This might have something to do with the fact that the form is the main form of the application and displayed via Application.run – user1283610 Mar 21 '12 at 14:43