转载:C#WinForm窗体事件执行次序(较完整版)

一、以下是网络上可搜索到的次序

   当 Windows Form 应用程序启动时,会以下列顺序引发主要表单的启动事件:
        System.Windows.Forms.Control.HandleCreated
        System.Windows.Forms.Control.BindingContextChanged
        System.Windows.Forms.Form.Load
        System.Windows.Forms.Control.VisibleChanged
        System.Windows.Forms.Form.Activated
        System.Windows.Forms.Form.Shown

    当应用程序关闭时,会以下列顺序引发主要表单的关闭事件:          
        System.Windows.Forms.Form.Closing
        System.Windows.Forms.Form.FormClosing
        System.Windows.Forms.Form.Closed
        System.Windows.Forms.Form.FormClosed
        System.Windows.Forms.Form.Deactivate

 

二、以下是我测试的次序,全部protected override 这些事件,并且在其base.的前后分别处理一次,如下

 

[c-sharp]   view plain copy
  1. protected override void OnLoad(EventArgs e)  
  2. {  
  3.     textBox1.Text += "OnLoad1" + "/r/n";  
  4.     base.OnLoad(e);  
  5.     textBox1.Text += "OnLoad2" + "/r/n";  
  6. }  

 

 

OnClientSizeChanged1
OnClientSizeChanged2
OnClientSizeChanged1
OnClientSizeChanged2

// Loyout要多次执行
OnLayout1  
OnLayout2
OnHanleCreated1
OnHanleCreated2
OnInvalidated1
OnInvalidated2

// 注意这里的一点点变化
OnCreateControl1
OnLoad1
OnLoad2
OnCreateControl2

// 
OnLayout1
OnLayout2
OnActivated1
OnActivated2
OnShown1
OnShown2
OnPain1
OnPain2

 

希望这个次序能给大家带来用处。。可以在不同事件中去处理所需要的代码

三、以下是代码源。C# 2008 Express

[c-sharp]   view plain copy
  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 WindowsFormsApplication1  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         // 输出窗体事件的执行次序  
  20.   
  21.         protected override void OnActivated(EventArgs e)  
  22.         {  
  23.             textBox1.Text +=  "OnActivated1" + "/r/n";  
  24.             base.OnActivated(e);  
  25.             textBox1.Text += "OnActivated2" + "/r/n";  
  26.         }  
  27.   
  28.         protected override void OnClientSizeChanged(EventArgs e)  
  29.         {  
  30.             textBox1.Text += "OnClientSizeChanged1" + "/r/n";  
  31.             base.OnClientSizeChanged(e);  
  32.             textBox1.Text += "OnClientSizeChanged2" + "/r/n";  
  33.         }  
  34.   
  35.         protected override void OnCreateControl()  
  36.         {  
  37.             textBox1.Text += "OnCreateControl1" + "/r/n";  
  38.             base.OnCreateControl();  
  39.             textBox1.Text += "OnCreateControl2" + "/r/n";  
  40.         }  
  41.   
  42.         protected override void OnDeactivate(EventArgs e)  
  43.         {  
  44.             textBox1.Text += "OnDeactivate1" + "/r/n";  
  45.             base.OnDeactivate(e);  
  46.             textBox1.Text += "OnDeactivate2" + "/r/n";  
  47.         }  
  48.   
  49.         protected override void OnHandleCreated(EventArgs e)  
  50.         {  
  51.             textBox1.Text += "OnHanleCreated1" + "/r/n";  
  52.             base.OnHandleCreated(e);  
  53.             textBox1.Text += "OnHanleCreated2" + "/r/n";  
  54.         }  
  55.   
  56.         protected override void OnHandleDestroyed(EventArgs e)  
  57.         {  
  58.             textBox1.Text += "OnHanleDestoryed1" + "/r/n";  
  59.             base.OnHandleDestroyed(e);  
  60.             textBox1.Text += "OnHanleDestoryed2" + "/r/n";  
  61.         }  
  62.   
  63.         protected override void OnInvalidated(InvalidateEventArgs e)  
  64.         {  
  65.             textBox1.Text += "OnInvalidated1" + "/r/n";  
  66.             base.OnInvalidated(e);  
  67.             textBox1.Text += "OnInvalidated2" + "/r/n";  
  68.         }  
  69.   
  70.         protected override void OnLayout(LayoutEventArgs levent)  
  71.         {  
  72.             textBox1.Text += "OnLayout1" + "/r/n";  
  73.             base.OnLayout(levent);  
  74.             textBox1.Text += "OnLayout2" + "/r/n";  
  75.         }  
  76.   
  77.         protected override void OnLoad(EventArgs e)  
  78.         {  
  79.             textBox1.Text += "OnLoad1" + "/r/n";  
  80.             base.OnLoad(e);  
  81.             textBox1.Text += "OnLoad2" + "/r/n";  
  82.         }  
  83.   
  84.         protected override void OnPaint(PaintEventArgs e)  
  85.         {  
  86.             textBox1.Text += "OnPain1" + "/r/n";  
  87.             base.OnPaint(e);  
  88.             textBox1.Text += "OnPain2" + "/r/n";  
  89.         }  
  90.   
  91.         protected override void OnShown(EventArgs e)  
  92.         {  
  93.             textBox1.Text += "OnShown1" + "/r/n";  
  94.             base.OnShown(e);  
  95.             textBox1.Text += "OnShown2" + "/r/n";  
  96.         }  
  97.   
  98.          
  99.     }  
  100. }  

转载于:https://www.cnblogs.com/TGRC/archive/2013/02/19/2916672.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值