什么设置代码应该放在表单构造函数和表单加载事件中? [英] What setup code should go in Form Constructors versus Form Load event?

查看:19
本文介绍了什么设置代码应该放在表单构造函数和表单加载事件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 winforms 应用程序,我想知道应该输入什么设置代码:

For winforms applications I'm wondering what setup code should go in:

  • 主窗体()

相对于

  • MainForm_Load(object sender, EventArgs e)

此处是否有任何最佳实践指南?

Are there any best practice guidelines here?

推荐答案

使用过 VB6 的程序员倾向于将大量代码放在 Load 事件中,在 VB6 中该事件用于初始化表单.但这在 Windows Forms 中不再合适,Form 类可以有一个构造函数..NET 方法是在构造函数中初始化类对象,很少有令人信服的理由不为 Form 类这样做.

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.

Load 事件在窗体的窗口句柄创建后立即运行,就在它对用户可见之前.您应该只在依赖于创建句柄的事件处理程序中编写代码.除了一种需要知道窗口大小和位置的代码之外,没有很多代码符合此要求.

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.

当表单在另一台机器上运行时,表单的设计时大小和位置属性值与其实际值不同.该表单可以重新缩放以适应系统字体大小或目标计算机上的视频适配器 DPI 设置.用户首选项也有影响,用户可能为窗口标题选择了不同的字体大小.您通常不关心这些,除非您希望窗口在桌面上具有特定位置或与其他某个窗口对齐.

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.

在 Load 事件中编写代码来执行诸如初始化 TreeView 或 ListView 控件之类的操作实际上会显着减慢启动时间.当您在构造函数中执行此操作时,Windows 窗体还不必更新物理窗口,它尚未创建.创建本机控件后,Winforms 将使用批量更新对其进行初始化,而不是在 Load 事件中运行代码时一次一个节点/项目.大不同.

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.

最后但并非最不重要的一点:你不应该使用 Load 事件,你应该覆盖 OnLoad() 方法.当您(或其他人)从您的 Form 类继承时,这可确保代码以可预测的顺序运行.IntelliSense 可帮助您编写此方法,只需键入protected onl"并按 Tab 键即可让 IntelliSense 自动完成该方法.请注意您如何选择在 base.OnLoad() 调用之前或之后放置代码,这就是您控制谁是老板的方式.当你追求它时,你就是老板,顺便说一句,这通常不是正确的选择.

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.

这篇关于什么设置代码应该放在表单构造函数和表单加载事件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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