在Windows窗体中加载类似事件的任何控件,例如文本框,复选框? [英] Load like event in Windows Forms for any control like textbox, checkbox?

查看:43
本文介绍了在Windows窗体中加载类似事件的任何控件,例如文本框,复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows Forms中,当首次显示UserControl或Form时,将触发Load事件.

In Windows Forms when a UserControl or Form first time becomes visible the Load event is fired.

http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.load.aspx

是否存在诸如Checkbox,TextBox,Label之类的控件的此类事件?

Is there any such event for controls like Checkbox, TextBox, Label ?

推荐答案

否.您可以使用HandleCreated事件,该事件在创建控件的本机窗口时触发.类构造函数运行后,您可以依靠运行的第一个事件.当父级将该控件添加到其Controls集合中并且该控件变为可见时,将触发该事件.

No. You could use the HandleCreated event, it is fired when the native window for the control is created. The first event you can rely on to run after the class constructor ran. It is triggered when the parent adds the control to its Controls collection and the control becomes visible.

但是请注意,此事件可能会触发多次.重新分配某些属性后,可能会重新创建控件,这种属性要求使用新的样式标志调用本机的CreateWindowEx()函数.因此,您至少需要随身携带一个布尔型标志,以保持对此状态的跟踪.

Beware however that it this event can fire more than once. Controls may be re-created when certain properties get reassigned, the kind that requires the native CreateWindowEx() function to be called with new style flags. So you'll at least need to carry around a bool flag that keeps track of this.

还请注意,在创建本机窗口后的 中设置控件的属性效率很低.所有Winforms控件都旨在允许在创建本机窗口之前 设置属性.无论您几乎要生成什么代码,都应该改用类构造函数.派生的控件本身.或在父代代码中,就像InitializeComponent()用于表单或用户控件一样.

Also note that setting properties of a control after the native window is created is pretty inefficient. All Winforms controls were designed to allow properties to be set before the native window is created. Whatever code you are generating almost surely should use the class constructor instead. Either of the derived control itself. Or in the code of the parent, much like InitializeComponent() does for a form or user-control.

对于现有的Load事件也是如此.由于VB6的遗留问题(Load事件非常重要),因此它往往被过度使用.但是,在Winforms中,仅依赖于控件或窗体的最终位置和大小的代码才需要.由于表单缩放,这可能与设计属性不同.任何其他代码都属于构造函数.

The same is true for the existing Load event. It tends to be over-used due to the VB6 legacy where the Load event was very important. In Winforms however it is only required for code that depends on the final location and size of a control or form. Which may be different from the design properties due to form scaling. Any other code belongs in the constructor.

这篇关于在Windows窗体中加载类似事件的任何控件,例如文本框,复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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