Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form.Load event triggered every time

Tags:

c#

windows

events

I was wondering if I am doing correctly.

I instantiate a Form (let's call this Form_B) within my class (also a form) and handle Form_B's Load event. Within this event I do some initialization.

Form_B can be displayed by the user multiple times, and I call ShowDialog on my instance variable.

The problem is that the Load is called each time I show the form. I tried debugging and also tried with Show() instead of ShowDialog(). Show() fails as I closed the window but ShowDialog() does not fail, but calls Load every time it is displayed.

Is it incorrect to continue using the instance once the form is closed?

Thanks, Stefan

like image 266
Vincent Avatar asked Mar 15 '11 15:03

Vincent


People also ask

Which event is triggered while form loads?

The load event is called once all the components of the form are loaded. If you redisplay the form, its components load again and therefore the Load event is triggered once more.

What is form load event?

The Form Load Event in VB . NET. An important event you'll want to write code for is the Form Load event. You might want to, for example, set the Enabled property of a control to False when a form loads. Or maybe blank out an item on your menu.

What is Form_Load?

It's the method called when a form is first created and loaded for display, if the method is bound to that event.

Which event is executed first for form control?

The Form and Control classes expose a set of events related to application startup and shutdown. When a Windows Forms application starts, the startup events of the main form are raised in the following order: Control. HandleCreated.


1 Answers

Using the Load event to initialize a form is an anachronism from the VB6 days. It was really important back then, that unfortunately carried over in the design of the Winforms designer. It made Load the default event for a form.

That is however not the .NET way, you initialize a class object with the constructor. The only time you need to override OnLoad() (another .NET way, events are for code in other classes) is when you care about the size and position of the form. It won't be the design Size and Location when the user changed the Windows theme or runs the video adapter at a higher DPI setting. So you might want to use OnLoad to move the window or rearrange the controls. Not actually a very common thing to do.

So, fix your problem first by using the constructor instead. If you still need OnLoad then just use a bool flag that keeps track of whether or not it already ran.

    private bool initialized = false;

    protected override void OnLoad(EventArgs e) {
        if (!initialized) {
            initialized = true;
            // etc...
        }
        base.OnLoad(e);
    }

And yes, this only works if you use ShowDialog(). A form that's displayed with Show() automatically disposes itself when it is closed. That doesn't happen with ShowDialog() to avoid problems retrieving the dialog results. Re-creating the dialog instance is the better way, unless you really care about keeping the last entered values. That's however a really expensive way to do so, form objects take a lot of .NET and Windows resources.

like image 108
Hans Passant Avatar answered Sep 27 '22 20:09

Hans Passant