C# C:如何告诉一个窗体另一个窗体已关闭?

C# C:如何告诉一个窗体另一个窗体已关闭?,c#,winforms,C#,Winforms,我有一个名为frmMain的表单,其中我有以下功能: public void openFullScreen(String id,String content) { frmEditor Editor = new frmEditor(); Editor.WindowState = FormWindowState.Maximized; Editor.Content = content; Editor.ID = id; Editor.ShowDialog(); }

我有一个名为frmMain的表单,其中我有以下功能:

public void openFullScreen(String id,String content)
{
    frmEditor Editor = new frmEditor();
    Editor.WindowState = FormWindowState.Maximized;
    Editor.Content = content;
    Editor.ID = id;
    Editor.ShowDialog();
}
在Editor.cs中,我使用以下代码:

private void btnClose_Click(object sender, EventArgs e)
{
    Object content = browserEditor.Document.InvokeScript("getContent");
    if (content != null)
    {
        object[] args = new object[2];
        args[0] = content.ToString();
        args[1] = _id;
        AppDomain.CurrentDomain.SetData("EditorContent", args);
        this.Close();
        //browserEditor.Document.InvokeScript("setEditorContent",args)
    }
}
关闭frmEditor时,我想告诉frmMain frmEditor现在已关闭,因为我知道必须显示某个值。如何检查此项?

只需订阅编辑器实例的FormClosed事件:

private void InitializeChildForm()
{
    var child = new ChildForm();
    child.FormClosed += ChildFormClosed;
    child.ShowDialog();
}

void ChildFormClosed(object sender, FormClosedEventArgs e)
{
    MessageBox.Show("Child form was closed.");
}
只需订阅编辑器实例的FormClosed事件:

private void InitializeChildForm()
{
    var child = new ChildForm();
    child.FormClosed += ChildFormClosed;
    child.ShowDialog();
}

void ChildFormClosed(object sender, FormClosedEventArgs e)
{
    MessageBox.Show("Child form was closed.");
}
该方法将一直阻止,直到对话框关闭

可以使用此方法在应用程序中显示模式对话框。调用此方法时,它后面的代码直到对话框关闭后才会执行。通过将对话框指定给窗体上按钮的DialogResult属性或在代码中设置窗体的DialogResult属性,可以为对话框指定DialogResult枚举的一个值。然后该方法返回该值

要返回结果,可以设置窗体中内置的属性。如果该类型不适合您的需要,请在编辑器中声明一个属性,并在ShowDialog返回时检索它

然后在openForm中

public void openFullScreen(String id,String content)
{
    frmEditor Editor = new frmEditor();
    Editor.WindowState = FormWindowState.Maximized;
    Editor.Content = content;
    Editor.ID = id;
    Editor.ShowDialog( this );
    string retval = Editor.YourReturnValue;
}
需要注意的重要一点是,仅仅因为表单是关闭的,并不意味着对象已经被破坏。当编辑器变量在作用域中时,它仍然可以访问

顺便说一句,我建议

该方法会一直阻塞,直到对话框关闭

可以使用此方法在应用程序中显示模式对话框。调用此方法时,它后面的代码直到对话框关闭后才会执行。通过将对话框指定给窗体上按钮的DialogResult属性或在代码中设置窗体的DialogResult属性,可以为对话框指定DialogResult枚举的一个值。然后该方法返回该值

要返回结果,可以设置窗体中内置的属性。如果该类型不适合您的需要,请在编辑器中声明一个属性,并在ShowDialog返回时检索它

然后在openForm中

public void openFullScreen(String id,String content)
{
    frmEditor Editor = new frmEditor();
    Editor.WindowState = FormWindowState.Maximized;
    Editor.Content = content;
    Editor.ID = id;
    Editor.ShowDialog( this );
    string retval = Editor.YourReturnValue;
}
需要注意的重要一点是,仅仅因为表单是关闭的,并不意味着对象已经被破坏。当编辑器变量在作用域中时,它仍然可以访问


顺便说一句,我建议

应该还点什么,不是吗?它会一直阻塞,直到所说的对话框关闭为止,所以只要等待就足够了。我知道ShowDialog返回值,在frmMain的哪个事件上我要检查它?只有在frmEditor完全关闭的情况下,您才能继续执行。否则执行将卡在ShowDialog方法中。因此,在此之后的任何代码都只会在关闭frmEditor后执行。ShowDialog阻塞,所以只需在关闭frmEditor之后执行。Editor.ShowDialog;当中介机构关闭时,进行其他操作;-可以使用此方法在应用程序中显示模式对话框。调用此方法时,它后面的代码直到对话框关闭后才会执行。。从上一个MSDN链接。要交换数据,您可以使用frmEditor中的公共变量来保存所需的数据。然后,在ShowDialog方法之后,您可以访问这些字段,并且您确信只有在关闭frmEditor之后才能访问它们,因为ShowDialog是一种阻塞方法。应该还点什么,不是吗?它会一直阻塞,直到所说的对话框关闭为止,所以只要等待就足够了。我知道ShowDialog返回值,在frmMain的哪个事件上我要检查它?只有在frmEditor完全关闭的情况下,您才能继续执行。否则执行将卡在ShowDialog方法中。因此,在此之后的任何代码都只会在关闭frmEditor后执行。ShowDialog阻塞,所以只需在关闭frmEditor之后执行。Editor.ShowDialog;当中介机构关闭时,进行其他操作;-可以使用此方法在应用程序中显示模式对话框。调用此方法时,它后面的代码直到对话框关闭后才会执行。。从上一个MSDN链接。要交换数据,您可以使用frmEditor中的公共变量来保存所需的数据。然后,在ShowDialog方法之后,您可以访问这些字段,并且您确信只有在关闭frmEditor之后才能访问它们,因为ShowDialog是一种阻塞方法。