即使添加处理程序后,Form1_Load 也不会触发

发布于 2024-10-20 02:51:48 字数 1401 浏览 0 评论 0原文

我已经很长时间没有接触过 C# 了,但我在启动 form_load 时遇到了很大的困难。这是最简单的事情,我无法想象为什么它不会火!任何帮助将不胜感激。

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");
        }
    }
}

更新

  • 我已使用断点来验证该行未命中
  • 表单确实显示 生成表单的屏幕截图

我也尝试了以下操作,但没有成功:

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);
        }
    }
}

更新 #2 我是可以通过删除并重新添加对 WebKit 控件的引用来实现此功能。不知道发生了什么。谢谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

蓬勃野心 2024-10-27 02:51:48

我刚刚解决了同样的问题。根本原因应该是加载 Form1 时未触发 Form1_Load 事件。只需在设计器视图中打开Form1,单击Form1的标题,单击Form1属性下的“事件”标签,在属性列表中找到“加载”,您将在其右侧找到事件列表。选择“Form1_Load”,重建它。您可以通过选择 Form1_Load 以外的任何事件来检查 Form1_Load() 是否被调用来进行验证。

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.

早茶月光 2024-10-27 02:51:48

解决方法如下:

直接在 InitializeComponent(); 之后插入代码。
调用此函数后,表单的私有字段将被初始化,您可以与 UI 对象进行交互。

我知道这并不能直接回答问题,但在大多数情况下应该有效。

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.

初心未许 2024-10-27 02:51:48

我来这里是为了完全相同的问题。我一看到更新:

我可以通过删除并重新添加对 WebKit 控件的引用来实现此目的。不知道发生了什么。谢谢大家。

我意识到我的情况出了什么问题。和 Scott 一样,我有一段时间没有写过任何 c# 了,并且忘记了一个关键细节。

我想要一些简单的示例代码来检查 listview 是否按照我需要的方式工作。最好的文档是工作代码,因此我从 https://www.dotnetperls.com/listview 复制了一个示例。启动 VS2010 并将代码粘贴到 Visual Studio 为我生成的表单程序中。运行它并得到与 Scott 完全相同的结果 - 带有空白列表的空白表格。

原因是在 Visual Studio 中,当您将控件添加到表单时,您不会自动获取所有事件处理程序 - form_load 是缺少的处理程序之一。您获得了绘制控件的代码,仅此而已。要告诉 Windows 您有表单加载事件的处理程序,您必须添加

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

到表单的InitializeComponent 中的代码。当我将控件添加到表单时,该行丢失了,因此我的处理程序没有被调用。

您可以手动添加该行,也可以进入 Visual Studio 中的设计窗口并单击表单的空白部分。您将看到自动生成的新空存根,如下所示:

private void Form1_Load_1(对象发送者,EventArgs e)

{

}

下划线-1 被添加到名称中,因为我已经有了无法正常运行的 Form1_Load 例程。

如果您随后检查 form1.designer.cs 代码,您将看到 this.load += .... 行已添加到初始化代码中。

有时简单的示例代码并不那么简单。

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.

不羁少年 2024-10-27 02:51:48

一般更好的过程是处理内部事件的重写虚拟方法,而不是注册触发的事件。

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

  base.OnLoad(e);
}

如果这没有被调用的话会很有趣。

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.

吹泡泡o 2024-10-27 02:51:48

这是我能想象的最简单的事情,为什么它不会火!

我怀疑问题不在于它“不触发”,而是有问题的代码没有按照您怀疑的方式处理事情。

尝试在“*.Dock”行上设置断点。鉴于上面的代码,只要您显示此表单,就应该点击它。但是,由于它是一个 Form.Load 事件,因此只有通过 form.Show() 实际显示表单实例时才会发生这种情况。

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().

我可以通过删除并重新添加对 WebKit 控件的引用来实现此目的。不知道发生了什么,但 John Arlen 的评论引导我走向正确的方向。谢谢大家。

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.

用心笑 2024-10-27 02:51:48

它不触发的另一个原因是,如果您正在使用 DataBindings 并且由于更改属性名称或删除属性而出现错误。

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

转瞬即逝 2024-10-27 02:51:48

今天遇到这个问题,一开始很困惑。读完这篇文章和类似的建议解决方案后,我的谜团解开了。

首先:我如何快速确定我的问题 - 我将 this.Load 事件注册移至设计器生成的代码中的 InitializeComponent() 的第一行。然后就达到了我的断点。然后我为所有抛出的异常(包括本机异常)打开“break”。这让我直面问题。

在我的例子中,包含一个 Web 控件导致加载在构建之后的一个令人惊讶的早期时刻发生;我的代码是在假设页面最终在 UI 中激活时发生加载的情况下编写的。它依赖于尚未设置的状态变量。

其他问题,例如缺少依赖的延迟加载 DLL、上游 Load 处理程序中的异常等,都可以通过这种方式进行追踪。

修复后,我将 this.Load() 移回到原来的位置。

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.

浅黛梨妆こ 2024-10-27 02:51:48

虽然有点晚了,但只需双击 Form1.cs [Design] 选项卡上的标题,它就会创建 Form1_Load 函数,然后该函数就可以正常工作了。

手动编写它似乎不起作用,因为双击它时会创建连接,就像双击按钮、列表框等时会发生相同的功能一样。

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.

白昼 2024-10-27 02:51:48

突然遇到同样的问题,从发布模式更改为调试模式后得到修复,不知道为什么。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文