为什么关闭嵌套子对话框也关闭父对话框? [英] Why does closing a nested child dialog also close the parent dialog?

查看:465
本文介绍了为什么关闭嵌套子对话框也关闭父对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打开一个形式使用ShowDialog的一个模式对话框。此对话框又允许另一种形式被打开,再次使用的ShowDialog一个模态对话框。

I open a form as a modal dialog using ShowDialog. This dialog in turn allows another form to be opened as a modal dialog again using ShowDialog.

当关闭最里面的对话框,这使得它的父对话框关闭为好。为什么会发生这种情况,我该如何预防呢?

When the innermost dialog is closed, this causes its parent dialog to close as well. Why does this occur and how can I prevent it?

我创建了问题的一个Hello World版本来说明这一点。

I have created a hello world version of the problem to illustrate this.

表格1:

private void OpenForm2Button_Click(object sender, EventArgs e)
{
    Form2 testForm = new Form2();
    DialogResult dialogResult = new DialogResult();
    dialogResult = testForm.ShowDialog();
    MessageBox.Show("Form 2 returned: " + Convert.ToString(dialogResult));
}



表格2:

Form 2:

...
this.Form2OKButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Form2CancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
...
this.AcceptButton = this.Form2OKButton;
this.CancelButton = this.Form2CancelButton;
...
private void OpenForm3Button_Click(object sender, EventArgs e)
{
    Form3 testForm = new Form3();
    DialogResult dialogResult = new DialogResult();
    dialogResult = testForm.ShowDialog();
    MessageBox.Show("Form 3 returned: " + Convert.ToString(dialogResult));
}



表3:

Form 3:

...
this.Form3OKButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Form3CancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
...
this.AcceptButton = this.Form3OKButton;
this.CancelButton = this.Form3CancelButton;



重现步骤:

Steps to reproduce:


  • 单击打开表单2

  • 单击打开表格3

  • 单击取消

表3的DialogResult关闭==取消的预期,但表2还具有的DialogResult关闭==取消(不预期)。

Form 3 closes with DialogResult == Cancel as expected, but Form 2 also closes with DialogResult == Cancel (not expected).

推荐答案

编辑:

问题是这个(文件: Form2.Designer.cs ):

the problem is this one (file: Form2.Designer.cs):

this.OpenForm3Button.DialogResult = System.Windows.Forms.DialogResult.Cancel;

当你点击 OpenForm3Button ,后在 OpenForm3Button_Click 事件处理结束, form.DialogResult 自动设置为取消,它是封闭的。

when you click the OpenForm3Button, after the end of the OpenForm3Button_Click event handler, the form.DialogResult is automatically set to Cancel and it is closed.

重置 OpenForm3Button的的DialogResult 属性,它将按预期工作:)

Reset the DialogResult property of OpenForm3Button and it will work as expected :)

这篇关于为什么关闭嵌套子对话框也关闭父对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆