的Form_Load()“事件”或覆盖的OnLoad() [英] Form_Load() 'event' or Override OnLoad()

查看:277
本文介绍了的Form_Load()“事件”或覆盖的OnLoad()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有人试图解释它们之间的区别。更具体地说,例如使用场景的。

I would like someone to try and explain the difference between these. More specifically, example usage scenario's.

我重构了一些 Windows窗体 code和A 表格有一些code。在的Form_Load()事件,并在保护覆盖无效的OnLoad()事件调用 base.OnLoad(E); 现在,我立即追查和替代火,然后再触发了事件的版本。

I am refactoring some Windows Form code and a Form has some code in the Form_Load() event and also in a protected override void OnLoad() event that calls base.OnLoad(e); Now I traced it and the Override fires first and then immediately fires off the event version.

那么,哪一个典型的使用,为什么?

So, which one is typically used and why?

推荐答案

您应该始终覆盖的OnLoad()。使用事件只有在适当的时候的另一个的班会感兴趣的事件。这是什么事件是。所感兴趣的Load事件另一类是非常罕见的,只有真正有用的做窗口排列的东西。

You should always override OnLoad(). Using the event is only appropriate when another class would be interested in the event. Which is what events are for. Another class being interested in the Load event is very rare, only really useful to do window arrangement stuff.

不过,Load事件可以很好地设计和VB6的程序员都非常舒服。这不是可怕的错误,你只遇到麻烦的时候,你开始继承的形式和code不正确的顺序执行。

Still, the Load event works well with the designer and VB6 programmers are very comfortable with it. It isn't horribly wrong, you'd only get in trouble when you start inheriting the form and code doesn't run in the right order.

大多数code,现在被放置在Load事件确实属于在构造函数中。你只需要的OnLoad如果:

Most code that now gets put in the Load event really belongs in the constructor. You only need OnLoad if:

  • 您需要了解窗口的确切大小和位置。的OnLoad是最好的,窗口句柄被创建,用户preferences应用(标题和边框大小)和形式重新调整所指示的Form.AutoScaleMode财产。该窗口还不可见,一个很好的时间来移动窗口别的地方或安排的​​子控件。
  • 您有需要的Handle属性code。这是很微妙的,你不能总是告诉。有code一样,在构造函数是不健康的,被创建的窗口构造完成之前。它通常是一个好下场,但它可以使创建表单非常慢。从简单的调用堆栈窗口来诊断。
  • 要避免在MDI实现中的错误。如果您创建一个MDI子的父类的构造,那么你会得到重复的字形,可见的方式,最大限度的孩子。创建孩子的OnLoad代替。

这篇关于的Form_Load()“事件”或覆盖的OnLoad()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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