windows Form.Load 事件每次触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5314362/
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 triggered every time
提问by Vincent
I was wondering if I am doing correctly.
我想知道我是否做得正确。
I instantiate a Form (let's call this Form_B) within my class (also a form) and handle Form_B's Load event. Within this event I do some initialization.
我在我的类(也是一个表单)中实例化一个 Form(我们称之为 Form_B)并处理 Form_B 的 Load 事件。在这个事件中,我做了一些初始化。
Form_B can be displayed by the user multiple times, and I call ShowDialog on my instance variable.
Form_B 可以由用户多次显示,我在我的实例变量上调用 ShowDialog。
The problem is that the Load is called each time I show the form. I tried debugging and also tried with Show() instead of ShowDialog(). Show() fails as I closed the window but ShowDialog() does not fail, but calls Load every time it is displayed.
问题是每次显示表单时都会调用 Load。我试过调试,也试过用 Show() 而不是 ShowDialog()。Show() 在我关闭窗口时失败,但 ShowDialog() 没有失败,而是在每次显示时调用 Load。
Is it incorrect to continue using the instance once the form is closed?
表单关闭后继续使用实例是否不正确?
Thanks, Stefan
谢谢,斯蒂芬
回答by Hans Passant
Using the Load event to initialize a form is an anachronism from the VB6 days. It was really important back then, that unfortunately carried over in the design of the Winforms designer. It made Load the default event for a form.
使用 Load 事件初始化表单是 VB6 时代的不合时宜。这在当时非常重要,不幸的是,它在 Winforms 设计师的设计中得以延续。它使 Load 成为表单的默认事件。
That is however not the .NET way, you initialize a class object with the constructor. The onlytime you need to override OnLoad() (another .NET way, events are for code in otherclasses) is when you care about the size and position of the form. It won't be the design Size and Location when the user changed the Windows theme or runs the video adapter at a higher DPI setting. So you might want to use OnLoad to move the window or rearrange the controls. Not actually a very common thing to do.
然而,这不是 .NET 方式,您使用构造函数初始化一个类对象。该只需要重写的OnLoad()时间(其他.NET方式,事件是在代码中的其他类)是当你关心的窗体的大小和位置。当用户更改 Windows 主题或以更高的 DPI 设置运行视频适配器时,它不会是设计大小和位置。因此,您可能希望使用 OnLoad 移动窗口或重新排列控件。实际上这不是一件很常见的事情。
So, fix your problem firstby using the constructor instead. If you still need OnLoad then just use a bool flag that keeps track of whether or not it already ran.
因此,首先通过使用构造函数来解决您的问题。如果您仍然需要 OnLoad,那么只需使用一个 bool 标志来跟踪它是否已经运行。
private bool initialized = false;
protected override void OnLoad(EventArgs e) {
if (!initialized) {
initialized = true;
// etc...
}
base.OnLoad(e);
}
And yes, this only works if you use ShowDialog(). A form that's displayed with Show() automatically disposes itself when it is closed. That doesn't happen with ShowDialog() to avoid problems retrieving the dialog results. Re-creating the dialog instance is the better way, unless you reallycare about keeping the last entered values. That's however a really expensive way to do so, form objects take a lotof .NET and Windows resources.
是的,这仅在您使用 ShowDialog() 时才有效。使用 Show() 显示的表单在关闭时会自动处理自身。ShowDialog() 不会发生这种情况,以避免检索对话框结果时出现问题。重新创建对话框实例是更好的方法,除非您真的很在意保留最后输入的值。然而,这是一种非常昂贵的方法,表单对象会占用大量.NET 和 Windows 资源。
回答by Robert MacLean
That is the correct behaviour of the Load event, each time it is loaded it is called. If you want to reuse the form and avoid the the Load event, rather than close the form you should hide it and use the show method to bring it out when needed.
这是 Load 事件的正确行为,每次加载时都会调用它。如果您想重用表单并避免 Load 事件,而不是关闭表单,您应该隐藏它并在需要时使用 show 方法将其显示出来。
回答by Anthony Vallée-Dubois
The load event is called once all the components of the form are loaded. If you redisplay the form, its components load again and therefore the Load event is triggered once more.
一旦加载了表单的所有组件,就会调用 load 事件。如果您重新显示表单,它的组件将再次加载,因此再次触发 Load 事件。
You could trigger a custom event that would only be triggered in your form's constructor if that's what you're looking for but I think it's bad practice to use a form after it's been closed.
您可以触发一个自定义事件,如果这是您要查找的内容,该事件只会在表单的构造函数中触发,但我认为在关闭表单后使用表单是不好的做法。
回答by haiduong87
I'm having the same problem. After searching awhile, I think the "ShowDialog" is an exception.
我有同样的问题。搜索了一段时间后,我认为“ShowDialog”是一个例外。
Since it's 2018 right now, MS has opened .Net. I've checked the source and found this.
现在是 2018 年,MS 已经打开了 .Net。我检查了来源并找到了这个。
this.CalledOnLoad = false;
this.CalledMakeVisible = false;
in the ShowDialog() function.
在 ShowDialog() 函数中。