Winforms - 加载和激活事件的顺序 [英] Winforms - order of Load and Activated events

查看:109
本文介绍了Winforms - 加载和激活事件的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的一位用户发送了一个.NET Winforms应用程序的日志,表示已激活事件发生之前 加载事件。我不认为这是可能的,并且假设加载将始终在激活之前进行编码。 p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $?????????????????????????????????????



如果是,为什么和有什么办法确保不会发生?

解决方案

Windows中的事件顺序表单 MSDN:


应用程序启动和关闭
事件



窗体和控件类暴露一个
与事件相关的事件
启动和关闭。当Windows
表单应用程序启动时,主窗体的启动
事件将按以下顺序在
中生成:



系统。 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.Swnwn



当应用程序关闭时,
主要形式的关闭事件是
按以下顺序升级:



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




您是否使用任何MessageBox你的启动事件?由于Windows窗体消息循环处理对话窗口的方式,这可能会导致事件触发不正常。


One of our users has sent in a log for our .NET Winforms application that indicates that the Activated event is occurring before the Load event. I didn't think this was possible and have coded with the assumption that Load would always happen before Activated.

Has anyone else observed Activated occurring before Load?

If so, why and is there any way to make sure it doesn't happen?

解决方案

From Order of Events in Windows Forms at MSDN:

Application Startup and Shutdown Events

The Form and Control classes expose a set of events related to application startup and shutdown. When a Windows Forms application starts, the startup events of the main form are raised in the following order:

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

When an application closes, the shutdown events of the main form are raised in the following order:

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

Are you using a MessageBox in any of your startup events? This can cause the events to appear to trigger out of order because of the way the Windows Forms Message Loop handles dialog windows.

这篇关于Winforms - 加载和激活事件的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆