关于Form_load()和OnLoad()使用中的联系与区别
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace OnLoadDemo 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 MessageBox.Show("Form_Load"); 22 } 23 protected override void OnLoad(EventArgs e) 24 { 25 //base.OnLoad(e); 26 MessageBox.Show("OnLoad"); 27 } 28 } 29 }
仅仅弹出“OnLoad”,Form1_Load()函数没有加载;
现在,注释掉base.OnLoad(e);运行之后,看一下情况如何变化:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace OnLoadDemo 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 MessageBox.Show("Form_Load"); 22 } 23 protected override void OnLoad(EventArgs e) 24 { 25 base.OnLoad(e); 26 MessageBox.Show("OnLoad"); 27 } 28 } 29 }
先出现From1_Load,如下
点击“确定”后,紧接着弹出OnLoad
综合以上两个示例:
base.onload(e);会引发事件调用Form1_Load()函数;如果重写了onload()函数,那么基类就不会引发From1_Load()事件;
事件是由委托构成的,如果事件没有触发,则delegate.BeginInvoke()就不会运行,delegate.BeginInvoke()不运行就不触发消息传递;
因为OnShown()事件是没有windows消息的,也就进一步说明,没有base.onload(e);的话OnShown()就无法运行,具体如下
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace LifeCycleDemo 9 { 10 class Program:Form 11 { 12 static void Main(string[] args) 13 { 14 Application.Run(new Program()); 15 } 16 public Program() 17 { 18 Text = "LifeCycleDemo"; 19 } 20 21 int inder = 0; 22 23 protected override void OnLoad(EventArgs e) 24 { 25 //base.OnLoad(e); 26 Console.WriteLine("the order of OnLoad event is {0}",++inder); 27 } 28 protected override void OnPaint(PaintEventArgs e) 29 { 30 Console.WriteLine("the order of onPaint event is {0}", ++inder); 31 } 32 protected override void OnShown(EventArgs e) 33 { 34 Console.WriteLine("the order of OnShow event is {0}", ++inder); 35 MessageBox.Show("Are you ready ?"); 36 } 37 //protected override void OnActivated(EventArgs e) 38 //{ 39 // Console.WriteLine("onActivated 事件的顺序是{0}", ++inder); 40 //} 41 //protected override void OnDeactivate(EventArgs e) 42 //{ 43 // Console.WriteLine("onDeactive 事件的顺序是{0}", ++inder); 44 //} 45 //protected override void OnFormClosed(FormClosedEventArgs e) 46 //{ 47 // Console.WriteLine("onFormClosed 事件的顺序是{0}", ++inder); 48 //} 49 //protected override void OnFormClosing(FormClosingEventArgs e) 50 //{ 51 // Console.WriteLine("onFormClosing 事件的顺序是{0}", ++inder); 52 //} 53 } 54 }
因为注释掉了base.onload(e);所以,onshown()也没有触发;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace LifeCycleDemo 9 { 10 class Program:Form 11 { 12 static void Main(string[] args) 13 { 14 Application.Run(new Program()); 15 } 16 public Program() 17 { 18 Text = "LifeCycleDemo"; 19 } 20 21 int inder = 0; 22 23 protected override void OnLoad(EventArgs e) 24 { 25 base.OnLoad(e); 26 Console.WriteLine("the order of OnLoad event is {0}",++inder); 27 } 28 protected override void OnPaint(PaintEventArgs e) 29 { 30 Console.WriteLine("the order of onPaint event is {0}", ++inder); 31 } 32 protected override void OnShown(EventArgs e) 33 { 34 Console.WriteLine("the order of OnShow event is {0}", ++inder); 35 MessageBox.Show("Are you ready ?"); 36 } 37 //protected override void OnActivated(EventArgs e) 38 //{ 39 // Console.WriteLine("onActivated 事件的顺序是{0}", ++inder); 40 //} 41 //protected override void OnDeactivate(EventArgs e) 42 //{ 43 // Console.WriteLine("onDeactive 事件的顺序是{0}", ++inder); 44 //} 45 //protected override void OnFormClosed(FormClosedEventArgs e) 46 //{ 47 // Console.WriteLine("onFormClosed 事件的顺序是{0}", ++inder); 48 //} 49 //protected override void OnFormClosing(FormClosingEventArgs e) 50 //{ 51 // Console.WriteLine("onFormClosing 事件的顺序是{0}", ++inder); 52 //} 53 } 54 }
启用base.onload(e);后,onshown()就会引起触发并运行;