c# - 子窗体关闭后运行代码

标签 c#

所以我有这个代码

public void Update_Click(object sender, EventArgs e)
{
    using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
    {
        menu.Items[2].Enabled = false;
        ShowProgress.ShowDialog();
        ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);
    }
}

public void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
    updaterAccess();
    menu.Items[2].Enabled = true;
}

所以在我单击“更新”后,它将运行子窗体 Form1 这是:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    notifyIcon1.Visible = true;
    notifyIcon1.BalloonTipTitle = "Update Complete";
    notifyIcon1.BalloonTipText = "Successfully Update";
    notifyIcon1.ShowBalloonTip(500);
    timer1.Interval = 4000;
    timer1.Enabled = true;
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Start(); 
}

private void timer1_Tick(object sender, EventArgs e)
{
    notifyIcon1.Dispose();
    this.Close();
}

如您所见,它运行在一个带有计时器的后台 worker 上以关闭子 Form1

现在我的问题是,在关闭 Child Form1 之后,它没有运行 MyForm_FormClosed,它应该再次启用 menu.Items[2] 和 updaterAccess()

我想我的 mainForm 中遗漏了一些东西

最佳答案

在触发 ShowDialog 之前附加事件处理程序

public void Update_Click(object sender, EventArgs e)
    {
        using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
        {
            menu.Items[2].Enabled = false;
            ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed); //Attached the event handler before firing ShowDialog
            ShowProgress.ShowDialog();
        }
    }

关于c# - 子窗体关闭后运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750055/

相关文章:

c# - 检查是否安装了任何类型的 PDF 阅读器

c# - Azure 文件共享 ListFilesAndDirectoriesSegmentedAsync() 身份验证失败

c# - 为 WP 游戏库制作正确的 URI

C# 多线程与 SQL Server 数据库调用

c# - 在没有 Entity Framework 的情况下创建 Odata Web API 应用程序

c# - 如何根据 "is-subset"条件有效地修剪列表?

c# - 解决导致 C# 错误的参数的最佳方法

c# - 将 StringBuilder 传递给 Java 和 C# 中的方法之间的差异

c# - 模型和 View 模型之间的区别

c# - 如何处理数字文件c#