Winforms Form Constructor vs Load event(Winforms Form Constructor vs Load事件)
问题描述
加载表单时,代码需要执行诸如设置数据网格、组合框、设置标题等操作.我倾向于始终使用加载事件而不是新的(构造函数).是否有针对哪些活动最适合哪些活动的指南?
When a form loads, the code needs to do things like setup datagrids, comboboxes, set the title, etc. I've tended to always use the load event rather than the new (constructor). Are there any guidelines for which one is best for which activities?
推荐答案
对 InitializeComponent 的调用会自动插入到表单/页面的构造函数中.InitializeComponent 是自动生成的方法,
A call to InitializeComponent is automatically inserted in the constructor of your form/page. InitializeComponent is the auto-generated method that
- 在您的 winform/XAML 页面上创建各种 UI 元素
- 使用存储在资源文件中的值初始化它们的属性
因此,与 UI 安排/修改相关的任何内容都应在此调用之后进行.当您在 Form.OnLoad 的覆盖中执行此操作时,您可以确保 UI 已准备就绪(已调用 InitializeComponent)...所以我会投票支持 UI 的 OnLoad.
创建非 UI 成员,构造函数将是我首先要看的地方.
So anything related to UI arrangement/modifications should go after this call. When you do this in an override of Form.OnLoad , you're assured that the UI is ready to go (InitializeComponent has been called)... so I'd vote for sticking to OnLoad for UI.
Creating non-UI members, constructor would be the place I'd first look at.
这篇关于Winforms Form Constructor vs Load事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Winforms Form Constructor vs Load事件


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01