Winforms Form Constructor vs Load事件

Winforms Form Constructor vs Load event(Winforms Form Constructor vs Load事件)
本文介绍了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事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)