Form1_Load not firing even after adding handler

24,507

Solution 1

I was able to get this working by removing and re-adding the references to the WebKit Control. Not sure what happened, but the comment by John Arlen steered me in the right direction. Thanks everyone.

Solution 2

I just worked out the same issue. The root cause should be that Form1_Load event was not fired while Form1 was loaded. Just open the Form1 in Designer view, click the Form1's title, click 'Event' tag under property of Form1, find 'Load' in the property list, you'll find a list of events on right hand of it. Select 'Form1_Load', rebuild it. You can verify by choosing any event other than Form1_Load to check if Form1_Load() being called or not.

Solution 3

Here's a workaround:

Insert your code directly after InitializeComponent();.
After this call, the form's private fields are initialized and you can interact with UI objects.

I know that this doesn't answer the question directly, but it should work in most cases.

Solution 4

General much better procedure is to handle the overridden virtual methods for internal events rather than register for the fired event.

protected override void OnLoad(EventArgs e)
{
  // Your code here

  base.OnLoad(e);
}

Would be interesting if this wasn't called.

Solution 5

I came here for the exact same problem. As soon as I saw the update:

I was able to get this working by removing and re-adding the references to the WebKit Control. Not sure what happened. Thanks everyone.

I realized what was wrong in my case. Like Scott, I hadn't done any c# in awhile and had forgotten a key detail.

I wanted some simple sample code to check out whether listview worked the way I needed it to. The best documentation is working code so I copied a sample from https://www.dotnetperls.com/listview . Fired up VS2010 and pasted the code into the form program Visual Studio generated for me. Ran it and got exactly the same thing Scott did - a blank form with a blank list.

The reason is that in Visual Studio, when you add a control to a form you don't automatically get all the event handlers - form_load being one of the missing handlers. You get the code to draw the control and that's it. To tell Windows that you have a handler for the form load event, you have to add

this.Load += new System.EventHandler(this.Form1_Load);

to the code in InitializeComponent for the form. That line was missing when I added the control to the form so my handler wasn't getting called.

You can either add the line manually or go into the design window in Visual Studio and click on an empty portion of the form. You'll see a new empty stub autogenerated that looks like:

private void Form1_Load_1(object sender, EventArgs e)

{

}

The underscore-1 is tacked on to the name because I already had the non-functioning Form1_Load routine.

If you then check the form1.designer.cs code, you'll see the this.load += .... line has been added to the initialization code.

Sometimes simple sample code isn't so simple.

Share:
24,507
Dutchie432
Author by

Dutchie432

There is a lot I could write here. Generally speaking, I am a (90% self-taught) computer programmer specializing in business related software utilizing .NET, PHP, JS, AJAX, jQuery, and XCode.

Updated on August 27, 2021

Comments

  • Dutchie432
    Dutchie432 over 2 years

    It's been a long time since I've dabbled with C#, but I'm having a heck of a time getting my form_load to fire. This is the most simple thing I can't imagine why it won't fire! Any assistance would be appreciated.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace AppName_v4._0___TestRoom_Addon{
        public partial class Form1 : Form{
    
            public Form1() {
                InitializeComponent();
                this.Load += new EventHandler(this.Form1_Load); //FIRES!
            }
    
            private void Form1_Load(object sender, EventArgs e) {
                webKitBrowser1.Dock = DockStyle.Fill; //DOES NOT FIRE!
                webKitBrowser1.Navigate("http://192.168.0.10/?zoneid=11");
            }
        }
    }
    

    UPDATE

    • I have used breakpoints to verify the line is not hit
    • The form does show Screenshot of resulting Form

    I have also tried the following with no success:

    namespace AlphaEntry_v4._0___MailRoom_Addon{
        public partial class Form1 : Form{
    
            public Form1() {
                InitializeComponent();
    
            }
    
            protected override void OnLoad(EventArgs e)
            {
                webKitBrowser1.Dock = DockStyle.Fill;
                webKitBrowser1.Navigate("http://192.168.0.10/?zoneid=11");
    
                base.OnLoad(e);
            }
        }
    }
    

    UPDATE #2 I was able to get this working by removing and re-adding the references to the WebKit Control. Not sure what happened. Thanks everyone.

    • Rafa Bytes
      Rafa Bytes about 13 years
      Have you read through the information in the output window?
    • Dutchie432
      Dutchie432 about 13 years
      @Beaner: Yes I see the error, but the function isn't being called even without the webKitBrowser1.* code
    • Dutchie432
      Dutchie432 about 13 years
      @Beaner: CORRECTION: Those are old errors and do not appear now that I've cleared my output window. Still no Form1_Load()
    • John Arlen
      John Arlen about 13 years
      (1) If you remove WebKitBrowser1 from the form, does it work? (2) Does running with HandleExceptionsAsThrown show any odd exceptions being swallowed?
    • Dutchie432
      Dutchie432 about 13 years
      SON OF A B*!@#. Removing the control from the form resolved the problem. I'm not sure why the heck that would be. I am using WebKit.NET webkitdotnet.sourceforge.net
    • Tamschi
      Tamschi about 10 years
      I think I have an idea why the first one might not call the event: Maybe InitializeComponent(); is what calls the Load event? I couldn't find any precise information about this though.
    • Dutchie432
      Dutchie432 almost 10 years
      @Tamschi - The code was correct. The libraries had to be reincluded. See answer.
  • Dutchie432
    Dutchie432 about 13 years
    I should have specified, but I've used breakpoints to establish the code is never hit, as a matter of course. This is the only form in the project and is the startup object as well. The code you see here is the entire project thus far!
  • Reed Copsey
    Reed Copsey about 13 years
    @Dutchie432: Is the form being displayed?
  • Dutchie432
    Dutchie432 about 13 years
    I think I've implemented the code as you suggested, but still with no results. See above update.
  • Dutchie432
    Dutchie432 about 13 years
    Ended up not solving the problem, but a good pointer none-the-less. +1
  • toddmo
    toddmo over 9 years
    I added a break point in OnLoad, and found that I was getting an error there. This prevented the load event from being called. So thanks for the tip.
  • Resource
    Resource over 8 years
    I just had the same problem - form displaying without Form1_Load being called - and Stan your answer fixed it! Thanks!
  • Martin
    Martin almost 5 years
    I just confirmed this. I had a binding to a property that no longer existed on the respective class. VS does not give you any clue about this (at least I did not see any). After removing the incorrect binding the Load event work again.