1

我有一个名为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 现在已关闭,因为知道我必须显示某个值。我该如何检查?

4

3 回答 3

6

只需订阅FormClosedEditor 实例的事件:

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

void ChildFormClosed(object sender, FormClosedEventArgs e)
{
    MessageBox.Show("Child form was closed.");
}
于 2013-05-26T06:30:10.263 回答
6

ShowDialog方法阻塞,直到对话框关闭。

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

要返回结果,您可以设置DialogResult中内置的属性Form。如果该类型不适合您的需要,请在其中声明一个属性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当变量在范围内时,它仍然可以访问。

顺便说一句,我建议将所有者传递给 ShowDialog

于 2013-05-26T06:31:56.187 回答
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)
    }
}
于 2013-05-26T11:35:38.470 回答