表单构造函数与表单加载事件中应该包含哪些设置代码?
对于 winforms 应用程序,我想知道应该输入什么设置代码:
- MainForm()
而不是
- MainForm_Load(object sender, EventArgs e)
这里有任何最佳实践指南吗?
For winforms applications I'm wondering what setup code should go in:
- MainForm()
as opposed to
- MainForm_Load(object sender, EventArgs e)
Are there any best practice guidelines here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用过 VB6 的程序员倾向于在 Load 事件中放入大量代码,在 VB6 中该事件用于初始化表单。但这在 Windows 窗体中不再合适,Form 类可以有一个构造函数。 .NET 的方式是在构造函数中初始化类对象,对于 Form 类来说,没有什么令人信服的理由不这样做。
Load 事件在窗体的窗口句柄创建之后、用户可见之前立即运行。您应该只在事件处理程序中编写依赖于创建句柄的代码。符合此要求的代码并不多,除了一种:需要知道窗口大小和位置的代码。
当窗体在另一台计算机上运行时,窗体的设计时 Size 和 Location 属性值与其实际值不同。可以重新调整表单以适应系统字体大小或目标计算机上的视频适配器 DPI 设置。用户偏好也发挥了作用,用户可能为窗口标题选择了不同的字体大小。您通常不关心这些,除非您希望窗口在桌面上具有特定位置或与其他窗口对齐。
在 Load 事件中编写代码来执行诸如初始化 TreeView 或 ListView 控件之类的操作实际上会大大减慢启动时间。当您在构造函数中执行此操作时,Windows 窗体还不必更新物理窗口,因为它尚未创建。创建本机控件后,Winforms 会通过批量更新来初始化它,而不是像代码在 Load 事件中运行时那样一次初始化一个节点/项。差别很大。
最后但并非最不重要的一点是:您永远不应该使用 Load 事件,您应该重写 OnLoad() 方法。这可以确保当您(或其他人)继承 Form 类时代码以可预测的顺序运行。 IntelliSense 可以帮助您编写此方法,只需键入“protected onl”并按 Tab 键即可让 IntelliSense 自动完成该方法。请注意您如何选择将代码放在 base.OnLoad() 调用之前或之后,这就是您控制谁是老板的方式。当你把它放在后面时,你就是老板,顺便说一句,这通常不是正确的选择。
Programmers that have worked with VB6 tend to put a lot of code in the Load event, in VB6 that event was used to initialize the form. But that's not appropriate anymore in Windows Forms, the Form class can have a constructor. The .NET way is to initialize class objects in the constructor, there are very few compelling reason to not do so for the Form class.
The Load event runs right after the window handle for the form was created, just before it becomes visible to the user. You should only write code in the event handler that depends on having the handle created. There is not a lot of code that qualifies for this requirement except one kind: code that requires the window size and location to be known.
The design-time Size and Location property values of a Form are not the same as their actual values when the form runs on another machine. The form can get rescaled to accommodate the system font size or the video adapter DPI setting on the target machine. The user preferences play a role too, the user might have selected a different font size for the window caption. You don't typically care about any of this, unless you want the window to have a particular position on the desktop or be aligned with some other window.
Writing code in the Load event that does things like initialize TreeView or ListView controls can actually dramatically slow down the startup time. When you do it in the constructor, Windows Forms doesn't have to update the physical window yet, it hasn't been created yet. Once the native control gets created, Winforms initializes it with a bulk update instead of one node/item at a time as will happen when the code runs in the Load event. Big difference.
Last but not least: you should never use the Load event, you should override the OnLoad() method. This ensures code runs in a predictable order when you (or somebody else) inherits from your Form class. IntelliSense helps you write this method, just type "protected onl" and press tab to have IntelliSense auto-complete the method. Note how you have a choice to put code before or after the base.OnLoad() call, that's how you control who is the boss. You are the boss when you put it after, not often the correct choice btw.
快速浏览一下在 Windows 窗体中使用构造函数确保正确初始化
Have a quick look at Use Constructor in Windows Forms to Ensure Proper Initialization