Form_Load与构造函数 [英] Form_Load vs Constructor

查看:106
本文介绍了Form_Load与构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些逻辑,用App.config中的值填充UI控件。一个

几个复选框被选中或取消选中;和物品加载到

复选清单框中。


两个合理的地方放置此代码:

1.表格'的构造函数(或者在

构造函数中调用的方法)

2. Form_Load事件过程


什么是两个

选择所带来的重要考虑或权衡?


谢谢。

解决方案

史密瑟斯,


如果你把代码放在''的构造函数中,那么你必须确保你知道所有的表示子控件的对象(对象的

实例,而不是窗口句柄)已经创建(在
之后的InitializeComponent方法,通常,如果你正在使用

设计器生成的代码)。


在第一次显示表单之前调用Load事件,

但是之后表格是构建的,所以你知道孩子的续rols已经在这一点上创建了




-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


史密瑟斯 < A@B.com写了留言

新闻:uO ************** @ TK2MSFTNGP02.phx.gbl ...


>我有一些逻辑,用App.config中的值填充UI控件。选中或取消选中几个复选框;和项目加载到
检查列表框。


两个合理的地方放置此代码:

1.表单'的构造函数(或者在

构造函数中调用的方法)

2. Form_Load事件过程


有哪些重要注意事项或两个

选择需要权衡吗?


谢谢。





首选,如果你把它放在构造函数中,只需确保

在调用InitializeComponents后执行它()

>
" Smithers" < A@B.com写了留言

新闻:uO ************** @ TK2MSFTNGP02.phx.gbl ...


>我有一些逻辑,用App.config中的值填充UI控件。选中或取消选中几个复选框;和项目加载到
检查列表框。


两个合理的地方放置此代码:

1.表单'的构造函数(或者在

构造函数中调用的方法)

2. Form_Load事件过程


有哪些重要注意事项或这两个选择需要权衡吗?


谢谢。



< BLOCKQUOTE>"史密琴斯" < A@B.com写了留言

新闻:uO ************** @ TK2MSFTNGP02.phx.gbl ...


>我有一些逻辑,用App.config中的值填充UI控件。选中或取消选中几个复选框;和项目加载到
检查列表框。


两个合理的地方放置此代码:

1.表单'的构造函数(或者在

构造函数中调用的方法)

2. Form_Load事件过程


有哪些重要注意事项或这两个选择需要权衡吗?



使用OnLoad覆盖,事件就在那里*其他*对象可以获得

通知,一个对象通常不应该使用它'有自己的事件来获得

通知。


您可以使用构造函数,就像其他人所说的那样但是这就是

Load事件适用于IMO,在技术上更正确,将它放在那里。


Michael


I have some logic that populates UI controls with values from App.config. A
couple of checkboxes get checked or unchecked; and items loaded into a
checked list box.

Two reasonable places to put this code:
1. the form''s constructor (or in a method called from within the
constructor)
2. the Form_Load event procedure

What are the important considerations or tradeoffs entailed by those two
choices?

Thanks.

解决方案

Smithers,

If you place the code in the form''s constructor, then you have to make
sure that you know that all the objects representing the child controls (the
instances of the objects, not the window handles) have been created (after
the InitializeComponent method, generally, if you are using
designer-generated code).

The Load event is called before the form is shown for the first time,
but after the form is constructed, so you know the child controls have been
created at this point.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Smithers" <A@B.comwrote in message
news:uO**************@TK2MSFTNGP02.phx.gbl...

>I have some logic that populates UI controls with values from App.config. A
couple of checkboxes get checked or unchecked; and items loaded into a
checked list box.

Two reasonable places to put this code:
1. the form''s constructor (or in a method called from within the
constructor)
2. the Form_Load event procedure

What are the important considerations or tradeoffs entailed by those two
choices?

Thanks.


Hi,

Preference basically, if you put it in the constructor just make sure of
doing it after the call to InitializeComponents()

"Smithers" <A@B.comwrote in message
news:uO**************@TK2MSFTNGP02.phx.gbl...

>I have some logic that populates UI controls with values from App.config. A
couple of checkboxes get checked or unchecked; and items loaded into a
checked list box.

Two reasonable places to put this code:
1. the form''s constructor (or in a method called from within the
constructor)
2. the Form_Load event procedure

What are the important considerations or tradeoffs entailed by those two
choices?

Thanks.



"Smithers" <A@B.comwrote in message
news:uO**************@TK2MSFTNGP02.phx.gbl...

>I have some logic that populates UI controls with values from App.config. A
couple of checkboxes get checked or unchecked; and items loaded into a
checked list box.

Two reasonable places to put this code:
1. the form''s constructor (or in a method called from within the
constructor)
2. the Form_Load event procedure

What are the important considerations or tradeoffs entailed by those two
choices?

Use the OnLoad override instead, events are there so *other* objects can get
notifications, an object should not generally use it''s own events to get
notifications.

You can probably use the constructor as other''s have said but this is what
the Load event is for so IMO it is more technically correct to put it there.

Michael


这篇关于Form_Load与构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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