执行“Form_Shown”事件处理程序不止一次?

发布于 2024-12-11 02:54:09 字数 869 浏览 0 评论 0原文

我目前正在开发一个具有多种形式的 Windows 应用程序。我在其中一个表单中使用 Form_Shown 来执行一些代码,以在显示表单之前初始化(刷新)表单。

在 MSDN 上的 Form.Shown 事件中,它声明该事件仅在第一次显示表单时引发。但是,我希望每次在某些表单中调用 Form.Show() 时都能够执行代码来初始化表单。这是一个例子。

  1. 来自名为Game的表单。包含一个事件处理程序 Game_Shown 和一个按钮,单击该按钮会显示一个名为 Menu 的表单:

    private void btnMenu_Click(对象发送者,EventArgs e)
    {
        this.Hide();
        Formulaires.formMenu.Show();
    }
    
    私人无效Game_Shown(对象发送者,EventArgs e)
    {
        代码在这里...
        this.Refresh();
    }
    
  2. 来自名为 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.

  1. From a form named Game. Contains an event handler Game_Shown and a button that when clicked shows a form named Menu:

    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();
    }
    
  2. From the form named Menu. Contains a button that when clicked shows the form named Game:

    private void lblGame_Click(object sender, EventArgs e)
    {
        this.Hide();
        Formulaires.formGame.Show();
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-12-18 02:54:09

它的行为是经过设计的。

来自文档:
只要表单首次显示,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.

猫弦 2024-12-18 02:54:09

您想要的内容需要详细了解 WinForm 生命周期中发生的事件。我不知道,这可能记录在某处。

我就是这样找到答案的:

  1. 创建一个包含 2 个表单(主表单和辅助表单)的小型测试项目,
  2. 添加显示和隐藏按钮并确保其有效。
  3. Debug.Print("EventName") 添加到帮助程序表单的所有候选事件中。
  4. 查看输出窗口中的日志并选择您的事件。

候选事件包括 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:

  1. create a small test project with 2 forms (Main and helper)
  2. add the show and hide buttons and make sure it works.
  3. Add Debug.Print("EventName") to all the candidate events of the helper form.
  4. Look at the log in the output window and pick your event.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文