Form.Load 事件未触发,表单显示 [英] Form.Load event not firing, form showing

查看:37
本文介绍了Form.Load 事件未触发,表单显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我担心我的代码有明显的问题,但是我遇到了一种情况,即当我创建和显示我的表单时 Form.Load 事件没有触发.

I fear that there is something obviously wrong with my code, but I have come across a situation where the Form.Load event is not firing when I create and show my form.

表单没有子类化(因为我在某些搜索中看到了一些问题),并且当我在调试器中单步执行代码时没有抛出任何错误.

The form is not subclassed (as I've seen some problems with that in some searches), and I am not getting any errors thrown when I step through the code in the debugger.

我在 IDE 创建的表单加载函数(确实具有 Handles MyBase.Load 签名后缀)上设置了一个断点,但从未达到断点,并且表单确实显示和工作.

I have a break point set on the IDE-created form load function (which does have the Handles MyBase.Load signature suffix) but the breakpoint is never reached and the form does display and work.

表单在构造函数中被传递了三个参数,但是 IntializeComponent() 函数在其他任何事情完成之前被调用.

The form is passed three arguments in the constructor but the IntializeComponent() function is called before anything else is done.

代码:

Public Sub New(ByVal argA As Object, ByVal argB As Object, ByVal mode As FormMode)

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Other code here,
    ' No errors generated
    '

End Sub

表单加载函数如下,(但由于未触发事件,因此从未实际执行过).

The form load function is as follows, (but this is never actually executed as the event is not fired).

代码:

Private Sub frmInstrumentEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not argA Is Nothing Then ' argA set in constructor
          ' Operations using argA
    End If
End Sub

我可能会补充说我正在使用一些带有一些控件和 argA 对象的数据绑定,但如果这产生了错误,我想我会看到这个(我将 CLR Execpetions 设置设置为 Thown in调试器 > 异常窗口)

I might add I am using some databinding with some controls and the argA object, but if this was producing an error I thought I would have seen this (I have CLR Execpetions settings set to Thown in the debugger > exceptions window)

任何想法为什么会发生这种情况?

Any ideas why this might be occurring?

推荐答案

我刚刚遇到了类似的问题(在 Shown 事件中,不是 Load,而是在 root原因是一样的).原因隐藏在其中一个祖先的深处 - 抛出了一个未处理的 NullReferenceException 并且这个异常在某种程度上被静音"了.

I just had a similar issue (it was in Shown event, not Load, but the root cause is the same). The reason was hidden deep in one of the ancestors - there was an unhandled NullReferenceException thrown and this exception was somehow "muted".

我在使用 F11 进行大量调试后发现了它.

I found it after extensive debugging with F11.

但是...在写这个答案时,我发现了这个 在 SO 上发帖

But... when writing this answer I found this post on SO

只需在 Main() 方法中添加 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException).

Just add Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) in your Main() method.

如果您使用的是 64 位机器,它会为您提供解决方案(在我的情况下也可以).

If you're using a 64-bit machine, it provides you with the solution (it worked in my case, too).

这篇关于Form.Load 事件未触发,表单显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆