About the author

Related Articles

4 Comments

  1. 1
    haiduong87@email.null'

    haiduong87

    I’m having the same problem. After searching awhile, I think the “ShowDialog” is an exception.

    Since it’s 2018 right now, MS has opened .Net. I’ve checked the source and found this.

    this.CalledOnLoad = false;
    this.CalledMakeVisible = false;
    

    in the ShowDialog() function.

    https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,ab288b84e00f8282

    Reply
  2. 2
    Hans

    Hans Passant

    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.

    Reply
  3. 3
    Robert

    Robert MacLean

    That is the correct behaviour of the Load event, each time it is loaded it is called. If you want to reuse the form and avoid the the Load event, rather than close the form you should hide it and use the show method to bring it out when needed.

    Reply
  4. 4
    Anthony

    Anthony Vallée-Dubois

    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.

    You could trigger a custom event that would only be triggered in your form’s constructor if that’s what you’re looking for but I think it’s bad practice to use a form after it’s been closed.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2017 SolutionMmyself.com