简体   繁体   English

不是顶级窗体的窗体不能显示为模式对话框。 在调用showDialog之前,从任何父表单中删除该表单

[英]Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog

In my project, i have used to show the Form as dialog by using "ShowDialog()" method. 在我的项目中,我曾经使用“ ShowDialog()”方法将“窗体”显示为对话框。 But it throws below exception 但是它抛出异常

"Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog." “不是顶级表单的表单不能显示为模式对话框。调用showDialog之前,请从任何父表单中删除该表单。”

Form frm = new Form();
frm.ShowDialog();

Please let me know the reason of this exception. 请让我知道此异常的原因。 Is it possible? 可能吗?

Thanks. 谢谢。

If you are using MDI and Form is a child form then error is telling you that the child form is not top-level form which needs to interact with the parent form. 如果您使用的是MDI并且Form是子表单,则错误告诉您该子表单不是顶级表单,需要与父表单进行交互。 "A modal form means it must be closed or hidden before you can continue working with the rest of application". “模态表单意味着您必须先关闭或隐藏它,然后才能继续使用其余的应用程序”。 This is why using ShowDialog() will have this error and where @S.Petrosov's answer comes in and where you should use Show() instead. 这就是为什么使用ShowDialog()会出现此错误,以及@ S.Petrosov的答案出现在何处以及应该在何处使用Show()

You need to pass owner Form to Form.ShowDialog . 您需要将所有者Form传递给Form.ShowDialog As shown below: 如下所示:

public void ShowMyDialogBox()
{
    Form2 testDialog = new Form2();

    // Show testDialog as a modal dialog and determine if DialogResult = OK.
    if (testDialog.ShowDialog(this) == DialogResult.OK)
    {
    // Read the contents of testDialog's TextBox.
    this.txtResult.Text = testDialog.TextBox1.Text;
    }
    else
    {
        this.txtResult.Text = "Cancelled";
    }
    testDialog.Dispose();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM