ShowDialog()不会使窗口模态

时间:2021-09-20 11:05:46

I have a windows form that pops up a dialog box if certian conditions are met when the form loads. The problem is the window does not stay on top and I can still click thing on the parent. However, there is a button on the form that when pressed opens the same window, when I do this it works as expected (like a dialog window).

如果表单加载时满足certian条件,我有一个窗口弹出一个对话框。问题是窗口没有停留在顶部,我仍然可以单击父项上的内容。但是,窗体上有一个按钮,当按下时打开同一个窗口,当我这样做时,它按预期工作(如对话框窗口)。

Is there an issue with showing a dialog when a form is first loading?

首次加载表单时是否存在显示对话框的问题?

4 个解决方案

#1


Are you calling ShowDialog from the Form class? Because it will only set the parent window if called from another Form. Alternatively you can use the overload that has the IWin32Window parameter to specifically set the owner.

你是从Form类调用ShowDialog吗?因为它只会在从另一个Form调用时设置父窗口。或者,您可以使用具有IWin32Window参数的重载来专门设置所有者。

#2


can you explain the issue further as this is my code which do not show the form it self until the dialog has been closed either you set the parent or not

你可以进一步解释这个问题,因为这是我的代码,在对话框关闭之前,你没有显示它自己的形式你是否设置了父级

  private void Form1_Load(object sender, EventArgs e)
        {
            //your functionality goes here    
            AboutBox1 box = new AboutBox1();
            box.ShowDialog();
        }
    }

on the other side you can also check with TopMost property

另一方面,您还可以查看TopMost酒店

#3


The ShowDialog method needs to be called from the form that you want to be it's parent/owner in order for it to be modal to that form. Alternatively I believe you can set the owner of a dialog directly but I have never needed to do that.

需要从您希望成为父/所有者的表单中调用ShowDialog方法,以使其成为该表单的模态。或者我相信你可以直接设置对话框的所有者,但我从来不需要这样做。

#4


DaBomb,

To do what you want, you will have to call your modal dialog from the constructor of your main form, NOT from the Form_Load event.

要执行您想要的操作,您必须从主窗体的构造函数调用模式对话框,而不是从Form_Load事件调用。

Something like this:

像这样的东西:

    public Form1()
    {
        InitializeComponent();
        this.Show();
        Form2 popupForm = new Form2();
        popupForm.ShowDialog();
    }

#1


Are you calling ShowDialog from the Form class? Because it will only set the parent window if called from another Form. Alternatively you can use the overload that has the IWin32Window parameter to specifically set the owner.

你是从Form类调用ShowDialog吗?因为它只会在从另一个Form调用时设置父窗口。或者,您可以使用具有IWin32Window参数的重载来专门设置所有者。

#2


can you explain the issue further as this is my code which do not show the form it self until the dialog has been closed either you set the parent or not

你可以进一步解释这个问题,因为这是我的代码,在对话框关闭之前,你没有显示它自己的形式你是否设置了父级

  private void Form1_Load(object sender, EventArgs e)
        {
            //your functionality goes here    
            AboutBox1 box = new AboutBox1();
            box.ShowDialog();
        }
    }

on the other side you can also check with TopMost property

另一方面,您还可以查看TopMost酒店

#3


The ShowDialog method needs to be called from the form that you want to be it's parent/owner in order for it to be modal to that form. Alternatively I believe you can set the owner of a dialog directly but I have never needed to do that.

需要从您希望成为父/所有者的表单中调用ShowDialog方法,以使其成为该表单的模态。或者我相信你可以直接设置对话框的所有者,但我从来不需要这样做。

#4


DaBomb,

To do what you want, you will have to call your modal dialog from the constructor of your main form, NOT from the Form_Load event.

要执行您想要的操作,您必须从主窗体的构造函数调用模式对话框,而不是从Form_Load事件调用。

Something like this:

像这样的东西:

    public Form1()
    {
        InitializeComponent();
        this.Show();
        Form2 popupForm = new Form2();
        popupForm.ShowDialog();
    }