vb.net Form.Load 事件未触发,表单显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4129925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Form.Load event not firing, form showing
提问by ChrisAU
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.
我担心我的代码明显有问题,但我遇到过这样一种情况,即Form.Load
在创建和显示表单时事件没有触发。
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.
表单不是子类化的(因为我在某些搜索中看到了一些问题),并且当我在调试器中逐步执行代码时,我没有收到任何错误。
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.
我在 IDE 创建的表单加载函数(它有Handles MyBase.Load
签名后缀)上设置了一个断点,但从未到达断点并且表单确实显示和工作。
The form is passed three arguments in the constructor but the IntializeComponent()
function is called before anything else is done.
该表单在构造IntializeComponent()
函数中传递了三个参数,但在执行任何其他操作之前调用该函数。
Code:
代码:
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).
表单加载函数如下,(但由于未触发事件,因此从未实际执行)。
Code:
代码:
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
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)
我可能会补充说,我正在对某些控件和argA
对象使用一些数据绑定,但是如果这会产生错误,我想我会看到这一点(我在调试器 > 异常窗口中将 CLR Execpetions 设置设置为 Thown)
Any ideas why this might be occurring?
任何想法为什么会发生这种情况?
回答by Marko Juvan?i?
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".
我刚刚遇到了类似的问题(确实Shown
发生了,而不是Load
,但根本原因是相同的)。原因隐藏在一个祖先的深处 - 有一个未处理的NullReferenceException
抛出,这个异常以某种方式被“静音”。
I found it after extensive debugging with F11.
我在使用 F11 进行大量调试后找到了它。
But... when writing this answer I found this post on SO
但是......在写这个答案时,我在 SO 上找到了这篇文章
Just add Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
in your Main()
method.
只需添加Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
您的Main()
方法。
If you're using a 64-bit machine, it provides you with the solution (it worked in my case, too).
如果您使用的是 64 位机器,它会为您提供解决方案(它也适用于我的情况)。
回答by John Whiting
I had a similar problem. On opening the form for the first time, the load event would not be tiggered but on opening it the second time, all would be well. The problem tuned out to be one of my text boxes which was bound to a field that I had deleted from the database (sql server - I was using datasets, tableadaptors and bindingsources in a fairly standard way).
我有一个类似的问题。第一次打开表单时,加载事件不会触发,但第二次打开时,一切都会好起来。问题调整为我的文本框之一,该文本框绑定到我从数据库中删除的字段(sql server - 我以相当标准的方式使用数据集、表适配器和绑定源)。
Make sure that all the controls on your form that are databound have fields that exist in the dataset and that the dataset is an accurate reflection of the underlying database table (the easiest was to do this last bit is to use the "Configure data source with wizzard" button on the data sources window (menu -data - show data sources) and remove the table. Then use it again to add the table back- this should make sure all the data matches.
确保表单上数据绑定的所有控件都具有数据集中存在的字段,并且数据集准确反映了基础数据库表(最简单的方法是使用“配置数据源” wizzard”按钮(菜单 - 数据 - 显示数据源)并删除表。然后再次使用它来添加表 - 这应该确保所有数据匹配。
Hope this helps.
希望这可以帮助。
回答by polychromenz
OK I had the SAME problem (I think) and the clues here helped. It was databinding (sort of)
好的,我遇到了同样的问题(我认为),这里的线索有所帮助。这是数据绑定(有点)
I had properties of some controls bound to settings and when I delete these settings the form load event stopped running. Removed the bindings and now it is running again.
我将某些控件的属性绑定到设置,当我删除这些设置时,表单加载事件停止运行。删除了绑定,现在它再次运行。
回答by Matt
Here is another idea.
这是另一个想法。
What happens if you set all exception types (not just for the CLR) to be thrown instead of user-unhandled. Does the application break anywhere at all?
如果您将所有异常类型(不仅仅是 CLR)设置为抛出而不是用户未处理会发生什么。应用程序是否在任何地方中断?
Also, just to double check, you are in debug mode right?
另外,只是仔细检查一下,您处于调试模式对吗?
回答by Matt
The problem you are experiencing may be caused by the application needing to fully load the form before you can do the "other code." This could be due to the other code dealing with objects on the form that haven't finished loading. You could use a timer that gets enabled in the load function to execute the other code. This way you don't have any timing issues and you can first load the form, and then a split second later, run the code you want from the timer.
您遇到的问题可能是由于应用程序需要在您执行“其他代码”之前完全加载表单所致。这可能是由于处理表单上尚未完成加载的对象的其他代码所致。您可以使用在加载函数中启用的计时器来执行其他代码。这样你就没有任何计时问题,你可以先加载表单,然后一秒钟后,从计时器运行你想要的代码。
回答by Amr
I had a similar issue, the problem was a mistake in the databinding. Omit the code for databinding and give it a try. I think the load event handler will be hit. Then see what's wrong with the databinding part.
我有一个类似的问题,问题是数据绑定中的错误。省略数据绑定的代码并尝试一下。我认为加载事件处理程序会被命中。然后看看数据绑定部分有什么问题。
回答by Bubba
Is your windows form inheriting from a base page? If so, the base page probably also has a Form Load event handler. In that base page Form Load event handler you will probably find an exception that is being thrown. So it is exiting the base page form load event handler and not firing the form load event handler in your inherited windows form.
您的 Windows 窗体是从基页继承的吗?如果是这样,则基页可能还有一个 Form Load 事件处理程序。在该基页 Form Load 事件处理程序中,您可能会发现正在抛出的异常。所以它正在退出基本页面表单加载事件处理程序,而不是在继承的 Windows 表单中触发表单加载事件处理程序。
回答by ccampj
Had the same problem. Checked my data bindings, everything looked ok. Got to thinking, even though form was closed, maybe .NET wasn't sure (old days, sometimes forms were only hidden and not really closed).
有同样的问题。检查了我的数据绑定,一切正常。开始思考,即使表单已关闭,也许 .NET 也不确定(过去,有时表单只是隐藏而不是真正关闭)。
I added the event handler FormClosed and put a single line in it:
我添加了事件处理程序 FormClosed 并在其中添加了一行:
Private Sub frmScheduleInquiry_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
End Sub
Problem solved!
问题解决了!
回答by Coder
Solved....
解决了....
Have spent 4 hours and finally got clues from this answers. in my case i was having couple of TextBox control on the form bound to a BindingSource with respective column, i still have that bindingsource on the form but what was happened that i deleted one column from underlying database table so on the form there is still one TextBox exist pointing to that column with bindingsource, in fact there is no column like that because i deleted !..... this lead Form.load event was not firing ........finally fixed..
花了4个小时,终于从这个答案中得到了线索。在我的情况下,我在表单上有几个 TextBox 控件绑定到具有相应列的 BindingSource,我仍然在表单上有那个 bindingsource,但是发生了什么我从基础数据库表中删除了一列,所以在表单上仍然有一个TextBox 存在指向具有 bindingsource 的该列,实际上没有这样的列,因为我删除了!.....
thanks to all of you ..
感谢大家 ..
回答by user3765883
I had the exact same problem just happen to me. Turns out I had added some ApplicationsSettings properties to a form TextBox control, but later wanted to delete them. I thoughtI had cleared everything out, but obviously I didn't - and this was what caused the Form_Load() (and maybe other events as well) to not fire. Deleting and then re-adding the offending TextBox did the trick.
我遇到了完全相同的问题。原来我已经向表单 TextBox 控件添加了一些 ApplicationsSettings 属性,但后来想删除它们。我以为我已经清除了所有内容,但显然我没有 - 这就是导致 Form_Load()(可能还有其他事件)不触发的原因。删除然后重新添加有问题的 TextBox 就成功了。
Hope this helps
希望这可以帮助