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 print ?
  1. protectedoverridevoidOnLoad(EventArgse)
  2. {
  3. textBox1.Text+="OnLoad1"+"/r/n";
  4. base.OnLoad(e);
  5. textBox1.Text+="OnLoad2"+"/r/n";
  6. }
protected override void OnLoad(EventArgs e) { textBox1.Text += "OnLoad1" + "/r/n"; base.OnLoad(e); textBox1.Text += "OnLoad2" + "/r/n"; }

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 print ?
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.ComponentModel;
  4. usingSystem.Data;
  5. usingSystem.Drawing;
  6. //usingSystem.Linq;
  7. usingSystem.Text;
  8. usingSystem.Windows.Forms;
  9. namespaceWindowsFormsApplication1
  10. {
  11. publicpartialclassForm1:Form
  12. {
  13. publicForm1()
  14. {
  15. InitializeComponent();
  16. }
  17. //输出窗体事件的执行次序
  18. protectedoverridevoidOnActivated(EventArgse)
  19. {
  20. textBox1.Text+="OnActivated1"+"/r/n";
  21. base.OnActivated(e);
  22. textBox1.Text+="OnActivated2"+"/r/n";
  23. }
  24. protectedoverridevoidOnClientSizeChanged(EventArgse)
  25. {
  26. textBox1.Text+="OnClientSizeChanged1"+"/r/n";
  27. base.OnClientSizeChanged(e);
  28. textBox1.Text+="OnClientSizeChanged2"+"/r/n";
  29. }
  30. protectedoverridevoidOnCreateControl()
  31. {
  32. textBox1.Text+="OnCreateControl1"+"/r/n";
  33. base.OnCreateControl();
  34. textBox1.Text+="OnCreateControl2"+"/r/n";
  35. }
  36. protectedoverridevoidOnDeactivate(EventArgse)
  37. {
  38. textBox1.Text+="OnDeactivate1"+"/r/n";
  39. base.OnDeactivate(e);
  40. textBox1.Text+="OnDeactivate2"+"/r/n";
  41. }
  42. protectedoverridevoidOnHandleCreated(EventArgse)
  43. {
  44. textBox1.Text+="OnHanleCreated1"+"/r/n";
  45. base.OnHandleCreated(e);
  46. textBox1.Text+="OnHanleCreated2"+"/r/n";
  47. }
  48. protectedoverridevoidOnHandleDestroyed(EventArgse)
  49. {
  50. textBox1.Text+="OnHanleDestoryed1"+"/r/n";
  51. base.OnHandleDestroyed(e);
  52. textBox1.Text+="OnHanleDestoryed2"+"/r/n";
  53. }
  54. protectedoverridevoidOnInvalidated(InvalidateEventArgse)
  55. {
  56. textBox1.Text+="OnInvalidated1"+"/r/n";
  57. base.OnInvalidated(e);
  58. textBox1.Text+="OnInvalidated2"+"/r/n";
  59. }
  60. protectedoverridevoidOnLayout(LayoutEventArgslevent)
  61. {
  62. textBox1.Text+="OnLayout1"+"/r/n";
  63. base.OnLayout(levent);
  64. textBox1.Text+="OnLayout2"+"/r/n";
  65. }
  66. protectedoverridevoidOnLoad(EventArgse)
  67. {
  68. textBox1.Text+="OnLoad1"+"/r/n";
  69. base.OnLoad(e);
  70. textBox1.Text+="OnLoad2"+"/r/n";
  71. }
  72. protectedoverridevoidOnPaint(PaintEventArgse)
  73. {
  74. textBox1.Text+="OnPain1"+"/r/n";
  75. base.OnPaint(e);
  76. textBox1.Text+="OnPain2"+"/r/n";
  77. }
  78. protectedoverridevoidOnShown(EventArgse)
  79. {
  80. textBox1.Text+="OnShown1"+"/r/n";
  81. base.OnShown(e);
  82. textBox1.Text+="OnShown2"+"/r/n";
  83. }
  84. }
  85. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值