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) {
开发者_StackOverflow 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
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.
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.
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.
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.
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()
.
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.
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.
Another reason for it not to fire is if you are using DataBindings and having errors because you changed property names or removed properties.
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.
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.
Met the same issue suddenly, got fixed after changing from release to debug mode, not sure why.
精彩评论