c#:如何告诉一个窗体另一个窗体已关闭
本文关键字:窗体 一个 另一个 何告诉 | 更新日期: 2023-09-27 18:15:28
我有一个名为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)
}
}
ON关闭frmEditor我想告诉frmMain frmEditor现在已关闭,在知道我必须显示某个值时。我怎么检查这个?
ShowDialog方法阻塞,直到对话框关闭。
您可以使用此方法在应用程序中显示模态对话框。调用此方法时,直到对话框关闭后才执行其后的代码。对话框可以通过将对话框赋值给窗体上按钮的对话框sult属性或通过在代码中设置窗体的对话框sult属性来赋值。这个值然后由这个方法返回。
要返回结果,您可以设置Form
中内置的dialgresult属性。如果该类型不适合您的需要,请在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
变量在作用域中时,它仍然是可访问的。
订阅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.");
}
//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)
}
}