1

I have several forms of which the containing controls are created and/or initialized at runtime. I do this in the Load event, but still, when the form is displayed, I can still see some forms being initialized.

In my understanding the Load event is happening before the form is displayed, so this shouldn't be a problem.

What am I missing here or what can I do to prevent this?

David
  • 15,894
  • 22
  • 55
  • 66
  • 1
    It is a common mistake is assume that a form that paints slowly because it has a lot of controls is busy "initializing". Try minimizing and restoring it. – Hans Passant May 27 '13 at 07:44
  • @Hans, What do you suggest to do in code? The form is initialized correctly; I just don't want to see it happening. –  May 27 '13 at 07:49
  • 1
    Create your items inside the constructor instead of load. Just like auto-generated code InitializeComponent() that's inside the Form constructor. – loopedcode May 27 '13 at 08:16
  • hmm maybe try `override void OnLoad` instead of using `Form_Load` event – Jens Kloster May 27 '13 at 08:17
  • 1
    At this point you should have mentioned what you saw when you minimized and restored your form. If it still looks like it is "initializing" then you *know* that it has nothing to do with the Load event handler. A trick to make painting less noticeable is available in [this answer](http://stackoverflow.com/a/3718648/17034). – Hans Passant May 27 '13 at 08:22

3 Answers3

2

Try calling this.SuspendLayout(); at the beginning of your Load Event and call this.ResumeLayout(); in the last line of the Load event.

Andy
  • 3,997
  • 2
  • 19
  • 39
1

Create your items inside the constructor instead of load. Just like auto-generated code InitializeComponent() that's inside the Form constructor, you can create instances of your controls and other objects after that line as needed.

loopedcode
  • 4,863
  • 1
  • 21
  • 21
0

Try to make form invisible in costructor and after your runtime initialization in Load event handler is completed show it. Or you can try to override OnLoad() method in your custom form and put base.OnLoad() after your custom actions

Demarsch
  • 1,419
  • 19
  • 39