[C#.net]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
在Form1_Load之後自動執行事件
可以使用事件Form1_Activated或Form1_Shown
區別在於Form1_Activated先執行。Form1_Activated可能觸發多次。
Form1_Shown是所有控件加載完成後執行,只會觸發一次。如果控件設置了背景圖片,那麽控件的背景顏色是不顯示的。
1 public bool IsActivated = false; 2 private void Form1_Activated(object sender, EventArgs e) 3 { 4 textBox1.Text += "Form1_Activated" + System.Environment.NewLine;5 if (!IsActivated) 6 { 7 IsActivated = true; 8 panel2.Dock = DockStyle.Fill; 9 } 10 } 11 12 private void Form1_Shown(object sender, EventArgs e) 13 { 14 textBox1.Text += "Form1_Shown" + System.Environment.NewLine; 15 }
[C#.net]WinForm載入窗體完成後自動執行事件