关于 Windows 窗体 UserControl 的 Load 事件
如果 UserControl 始终发生在 InitializeComponent()
方法和构造函数完成之后,是否可以保证 Load
事件发生?
Is there a guarantee that the Load
event if an UserControl always occurs after the InitializeComponent()
method and the constructor have been completed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会。创建 Handle 后,Load 事件立即运行。构造函数可以使用需要创建物理窗口的控件的属性。这会自动触发 CreateHandle() 方法,接下来是 Load。
这种情况很少见,而且通常是一个错误。它往往会有一个好的结局,具体取决于事件处理程序中正在执行的操作。它应该只做那些需要窗口的事情。这并不常见,其他任何东西都属于构造函数。然而,构造函数尚未完成,因此您正在使用部分初始化的对象。发生事故是有可能的。
No. The Load event runs right after the Handle is created. It is possible for the constructor to use a property of a control that requires the physical window to be created. That automatically triggers the CreateHandle() method, Load is next.
This is rare and usually a mistake. It tends to come to a good end, depending on what is being done in the event handler. Which ought to only ever do the kind of things that require the window. That's not common, anything else belongs in the constructor. The constructor however hasn't finished yet so you are working with a partially initialized object. Accidents are possible.