Using the Load event to initialize a form is an anachronism from VB6 days. At that time it was really important, which, unfortunately, was done in the design of the Winforms designer. He has loaded the default event for the form.
However, this is not a .NET path; you initialize the class object using the constructor. The only time you need to override OnLoad () (another .NET way, events for code in other classes) is when you care about the size and position of the form. This will not be the size and location of the project when the user changes the Windows theme or launches the video adapter with a higher DPI value. This way you can use OnLoad to move the window or change the controls. This is actually not a very common thing.
So, first fix your problem using the constructor instead. If you still need OnLoad, just use the bool flag, which keeps track of whether it is already running.
private bool initialized = false; protected override void OnLoad(EventArgs e) { if (!initialized) { initialized = true;
And yes, this only works if you use ShowDialog (). The form displayed with Show () is automatically deleted when it is closed. This does not happen with ShowDialog () to avoid problems with getting the results of the dialog. Re-creating an instance of the dialog is the best way if you really don't care about saving the last entered values. However, a very expensive way to do this is to create objects that consume a lot of .NET and Windows resources.
source share