执行“Form_Shown”事件处理程序不止一次?
我目前正在开发一个具有多种形式的 Windows 应用程序。我在其中一个表单中使用 Form_Shown
来执行一些代码,以在显示表单之前初始化(刷新)表单。
在 MSDN 上的 Form.Shown 事件中,它声明该事件仅在第一次显示表单时引发。但是,我希望每次在某些表单中调用 Form.Show()
时都能够执行代码来初始化表单。这是一个例子。
来自名为
Game
的表单。包含一个事件处理程序Game_Shown
和一个按钮,单击该按钮会显示一个名为Menu
的表单:private void btnMenu_Click(对象发送者,EventArgs e) { this.Hide(); Formulaires.formMenu.Show(); } 私人无效Game_Shown(对象发送者,EventArgs e) { 代码在这里... this.Refresh(); }
来自名为
Menu
的表单。包含一个按钮,单击该按钮会显示名为Game
的表单:private void lblGame_Click(对象发送者,EventArgs e) { this.隐藏(); Formulaires.formGame.Show(); }
I am currently developing a Windows app with several forms. I use Form_Shown
in one of those forms to execute some code to initialize (refresh) the form before showing it.
In Form.Shown Event on MSDN, it states that the event is raised only the first time the form is shown. However, I want to be able to execute code to initialize my form every time that I call Form.Show()
in some of the forms. Here's an example.
From a form named
Game
. Contains an event handlerGame_Shown
and a button that when clicked shows a form namedMenu
:private void btnMenu_Click(object sender, EventArgs e) { this.Hide(); Formulaires.formMenu.Show(); } private void Game_Shown(object sender, EventArgs e) { Code here... this.Refresh(); }
From the form named
Menu
. Contains a button that when clicked shows the form namedGame
:private void lblGame_Click(object sender, EventArgs e) { this.Hide(); Formulaires.formGame.Show(); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它的行为是经过设计的。
来自文档:
只要表单首次显示,Shown 事件就会发生。
另外,您不应该在类中处理 Shown 事件,而应该重写 OnShown。
为了实现您想要的效果,您可以尝试重写 OnVisibleChanged 方法。在方法内部,如果表单可见,则执行您的代码。
与 Shown 事件一样,您不应在表单类中处理它,而是重写适当的方法:
来自文档:
OnVisibleChanged 方法还允许派生类在不附加委托的情况下处理事件。这是在派生类中处理事件的首选技术。
It is behaving by design.
From the docs:
The Shown event occurs whenever the form is first shown.
Also, you should not handle the Shown event in your class, rather you should override OnShown.
To achieve what you want, you might try overriding the OnVisibleChanged method. Inside the method, if the form is visible, then execute your code.
Like the Shown event, you should not handle it in your form class, instead override the appropriate method:
From the docs:
The OnVisibleChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.
您想要的内容需要详细了解 WinForm 生命周期中发生的事件。我不知道,这可能记录在某处。
我就是这样找到答案的:
Debug.Print("EventName")
添加到帮助程序表单的所有候选事件中。候选事件包括 FormClosing、FormClosed、(De)Activated、Enter、Leave、Load……浏览列表。
当您找到合适的答案时,请将其发布在此处。
What you want requires some detailed knowledge about which event happens when in the WinForm lifecycle. That may be documented somewhere, I don't know.
This is how I would find out:
Debug.Print("EventName")
to all the candidate events of the helper form.Candidate events would be FormClosing, FormClosed, (De)Activated, Enter, Leave, Load, ... go through the list.
When you find the right one, please post it here in an answer.