Form1_Load not firing even after adding handler
Asked Answered
T

10

6

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.

Tippet answered 4/3, 2011 at 18:22 Comment(7)
Have you read through the information in the output window?Soothfast
@Beaner: Yes I see the error, but the function isn't being called even without the webKitBrowser1.* codeTippet
@Beaner: CORRECTION: Those are old errors and do not appear now that I've cleared my output window. Still no Form1_Load()Tippet
(1) If you remove WebKitBrowser1 from the form, does it work? (2) Does running with HandleExceptionsAsThrown show any odd exceptions being swallowed?Flail
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.netTippet
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.Hospital
@Hospital - The code was correct. The libraries had to be reincluded. See answer.Tippet
T
3

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.

Tippet answered 4/3, 2011 at 19:26 Comment(0)
A
14

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.

Amalburga answered 9/11, 2015 at 6:30 Comment(1)
I just had the same problem - form displaying without Form1_Load being called - and Stan your answer fixed it! Thanks!Tacho
H
6

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.

Hospital answered 4/3, 2011 at 19:4 Comment(0)
P
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.

Pelvis answered 10/1, 2017 at 18:18 Comment(0)
F
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.

Flail answered 4/3, 2011 at 18:49 Comment(3)
I think I've implemented the code as you suggested, but still with no results. See above update.Tippet
Ended up not solving the problem, but a good pointer none-the-less. +1Tippet
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.Postdiluvian
T
3

This is the most simple thing I can imagine why it won't fire!

I suspect the problem is not that it "doesn't fire", but rather that the code in question is not handling things the way you suspect.

Try setting a breakpoint on the "*.Dock" line. Given the code above, it should be hit as soon as you show this form. However, as it's a Form.Load event, this won't happen until an instance of the form is actually displayed via form.Show().

Ton answered 4/3, 2011 at 18:32 Comment(2)
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!Tippet
@Dutchie432: Is the form being displayed?Ton
T
3

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.

Tippet answered 4/3, 2011 at 19:26 Comment(0)
N
1

Another reason for it not to fire is if you are using DataBindings and having errors because you changed property names or removed properties.

Nicobarese answered 5/7, 2015 at 3:8 Comment(1)
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.Cloudland
C
0

Encountered this today, perplexing at first. After reading this and similar suggested resolutions, my mystery was unlocked.

First: how I quickly determined my issue - I moved the this.Load event registration up as the first line of InitializeComponent() in the designer-generated code. Then my breakpoint was reached. I then turned on "break" for all thrown exceptions, including native. This led me straight to the problem.

In my case inclusion of a web control caused the Load to occur at a surprisingly early moment, just after construction; where as my code was written with the assumption Load would occur when the page is finally activated in the UI. It was reliant on a state variable that was not yet set up.

Other problems, like lack of a dependent delay-loaded DLL, exception in an upstream Load handler and so on, can all be tracked down this way.

After fixing, I moved this.Load() back to its original location.

Crocked answered 4/5, 2017 at 14:48 Comment(0)
O
0

A little late to the party here, but by simply double clicking on the title on the Form1.cs [Design] tab it creates the Form1_Load function, which then works just fine.

Writing it manually does not seem to work as the hookups are created when you double click on it, much like the same functionality happens when you double click a button, listbox etc.

Oblige answered 21/4, 2021 at 22:13 Comment(0)
W
0

Met the same issue suddenly, got fixed after changing from release to debug mode, not sure why.

Wingover answered 26/8, 2021 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.