对于关注对 Windows 窗体应用程序中引发的每个事件按次序进行处理的开发人员来说,事件引发的顺序特别重要。当某种情况需要小心处理事件时(如重绘窗体的某些部分时),必须知道事件在运行时的确切引发顺序。
应用程序启动和关闭事件
Form 和 Control 类公开了一组与应用程序启动和关闭相关联的事件。当 Windows 窗体应用程序启动时,主窗体的启动事件按以下顺序引发:
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
Application 类的 ApplicationExit 事件在主窗体的关闭事件之后引发。
焦点和验证事件
当通过使用键盘(Tab、Shift+Tab 等等)、通过调用 Select 或 SelectNextControl 方法或通过设置当前窗体的 P:System.Windows.Forms.ContainerControl.ActiveControl 属性更改焦点时,Control 类的焦点事件按以下顺序发生:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
当通过使用鼠标或通过调用 Focus 方法更改焦点时,Control 类的焦点事件按以下顺序发生:
Enter
GotFocus
LostFocus
Leave
Validating
本文详细介绍了Windows窗体应用程序中事件的触发顺序,包括应用程序启动和关闭时的事件流程,以及焦点和验证事件的顺序。这些信息对于理解窗体如何响应用户交互至关重要。
1万+

被折叠的 条评论
为什么被折叠?



