C#:如何告诉表单其他表单已关闭?

时间:2013-05-26 06:25:41

标签: 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();
}

在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,知道我必须显示某个值。我该如何检查?

3 个答案:

答案 0 :(得分:6)

只需订阅编辑器实例的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.");
}

答案 1 :(得分:6)

ShowDialog方法会阻止对话框关闭。

  

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

要返回结果,您可以设置Form内置的DialogResult属性。如果该类型不符合您的需求,请在Editor中声明一个属性,并在ShowDialog返回时检索该属性。

public partial class Editor : Form
{
    public string YourReturnValue { get; private set; }

    private void btnClose_Click(object sender, EventArgs e)
    {
        // you code here...
        YourReturnValue = "Something you want to return";
    }
 }

然后在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;
}

重要的是要注意的是,仅仅因为窗体是封闭的,并不意味着对象已被破坏。 Editor变量在范围内时仍可访问。

顺便说一句,我建议passing an owner into ShowDialog

答案 2 :(得分:1)

//you can use DialogResult  object to know to other form is closed
// DialogResult dlgResult = DialogResult.None;

public void openFullScreen(String id,String content)
{
      DialogResult dlgResult = DialogResult.None;  

    frmEditor Editor = new frmEditor();`enter code here`
    Editor.WindowState = FormWindowState.Maximized;
    Editor.Content = content;
    Editor.ID = id;
    dlgResult=Editor.ShowDialog();
      if (dlgResult == System.Windows.Forms.DialogResult.OK)
      { 
               // code that you will execute after Editor form is closed
      }

}



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);

   /* use:  this.DialogResult = System.Windows.Forms.DialogResult.OK;   instead of this.close */
        this.DialogResult = System.Windows.Forms.DialogResult.OK;//this.Close();
        //browserEditor.Document.InvokeScript("setEditorContent",args)
    }
}