首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Windows Form中的构造函数和Form1.Load()有什么区别和联系

2013-10-05 
Windows Form中的构造函数跟Form1.Load()有什么区别和联系在创建一个窗体程序以后会出现楼主有下面几个问

Windows Form中的构造函数跟Form1.Load()有什么区别和联系
在创建一个窗体程序以后会出现Windows Form中的构造函数和Form1.Load()有什么区别和联系
楼主有下面几个问题:
1.  private void Form1_Load(object sender, EventArgs e)是函数还是事件;
2.  为什么 我把写在 private void Form1_Load(object sender, EventArgs e)中的代码写在构造函数中也能  。  被运行,这两者有是什么区别?
3.  楼主知道构造函数是创建窗体对象是发生的,而Form1_Load是在构造函数之后,窗体显示之前运行的,那为       。  什么交换代码也可以运行?
4.  Form1_Load和构造函数到底具体的功能是什么?
5.. 在两者发生的中间那段时间中有什么事情发生吗?
[解决办法]
没有构造函数中的
this.Load += Form_Load
Form1_Load还能执行么?
[解决办法]
构造函数调用了
InitializeComponent()
VS为InitializeComponent()产生了如下代码:
...
this.Load += new System.EventHandler(this.Form1_Load);
...
这才有了Form1_Load

先把基本语法搞清楚吧。
[解决办法]
程序环境初始化好所有的内容,然后设置Visible为true的时候,才会触发Load。因此你可以认为,这个事件说明基本上是万事俱备只欠东风了,你这这个事件进行自定义的操作不会遇到更多的诡异的异常初始化问题。

基本上,凡是需要在UI方面进行动态初始化的,都应该在这个事件去处理。以免初始化过早。
[解决办法]
比如过你要在界面上动态显示“汽车仪表”控件,而这个仪表显然是在其它控件的最顶层的,甚至它需要与你在设计时拖到设计窗口里的某个控件交互,那么你把这个功能放在别的控件初始化完备之前显然就会出现问题了。

在Form_Load中做的事情,很明显就是要等到其它的控件都初始好之后才加载的。
[解决办法]
实际上这个问题根本不用问,调试下,看下堆栈就知道了:
>WindowsFormsApplication1.exe!WindowsFormsApplication1.Form1.Form1_Load(object sender, System.EventArgs e) Line 22C#
 System.Windows.Forms.dll!System.Windows.Forms.Form.OnLoad(System.EventArgs e) + 0x1a5 bytesOnLoad调用了Form.Load,这个函数可以被重写
 System.Windows.Forms.dll!System.Windows.Forms.Form.OnCreateControl() + 0x58 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl(bool fIgnoreVisible) + 0x194 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.CreateControl() + 0x28 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WmShowWindow(ref System.Windows.Forms.Message m) + 0x88 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) + 0x2cb bytes
 System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.WndProc(ref System.Windows.Forms.Message m) + 0x2a bytes
 System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m) + 0x10 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Form.WmShowWindow(ref System.Windows.Forms.Message m) + 0x3d bytes
 System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m) + 0x209 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) + 0x11 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) + 0x39 bytes
 System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) + 0x5e bytes
 [Native to Managed Transition]
 [Managed to Native Transition]
 System.Windows.Forms.dll!System.Windows.Forms.Control.SetVisibleCore(bool value) + 0x3f5 bytes
 System.Windows.Forms.dll!System.Windows.Forms.Form.SetVisibleCore(bool value) + 0x7f bytes
 System.Windows.Forms.dll!System.Windows.Forms.Control.Visible.set(bool value) + 0x14 bytes
最下面是Visible = true
这个是Show或者ShowDialog触发的。

[解决办法]
1、任何一个类都会有构造函数,即使你没有写编译器也会隐式生成默认无参数构造函数,要调用一个类的任何方法(包括静态方法,静态方法会实例化一次静态构造),都会先调用构造函数(一个类的静态实例化在整个进程周期只调用一次)。
2、From窗体也是一个类,因此会先调用构造函数,而构造函数中的InitializeComponent()方法注册了Load事件,因此当触发Load事件时就会调用该事件注册的所有委托方法(
如: private void Form1_Load(object sender, EventArgs e)
),因此只要你明白构造函数、事件、委托等这些基本原理,那就懂得这些原理了。
3、如果VS没有提供相应设计器,都需要手写编写这些代码,那你就更容易懂得这些原理,而自动生成这些代码都是设计的功劳,所以很多初学者没有深入学习,每天都只是拖拖控件,双击控件生成相应事件,当遇到一些问题时总是无法解决,即使是简单的一些界面操作,设计器也可能会发生问题导致不正确的代码。以前的VS2003的From默认把所有代码都放在一文件里,但VS2005以后则把自动生成的代码与人工编写的代码分开存入不同的文件,这样更好方便管理代码,但很多初学者就不会去打开自动代码的文件,甚至不知道有那些代码。
[解决办法]
先后次序关系了,构造函数是用来初始化窗体实例的,只有有了窗体实例才存在Form_Load事件了,所以执行顺序是先调用构造函数初始化窗体再触发Load事件

热点排行