Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForms中的顶级窗口_C#_.net_Winforms_Dialog_Toplevel - Fatal编程技术网

C# WinForms中的顶级窗口

C# WinForms中的顶级窗口,c#,.net,winforms,dialog,toplevel,C#,.net,Winforms,Dialog,Toplevel,对于WinForms,有很多关于这方面的问题,但我没有看到一个提到以下场景 我有三种表格: (X) Main (Y) Basket for drag and drop that needs to be on top (Z) Some other dialog form X is the main form running the message loop. X holds a reference to Y and calls it's Show() method on load.

对于WinForms,有很多关于这方面的问题,但我没有看到一个提到以下场景

我有三种表格:

(X) Main  
(Y) Basket for drag and drop that needs to be on top  
(Z) Some other dialog form  

X is the main form running the message loop.
X holds a reference to Y and calls it's Show() method on load.
X then calls Z.ShowDialog(Z).
现在,在Z关闭之前,Y不再可访问


我多少能理解为什么(不是真的)。有没有一种方法可以使Y保持浮动,因为最终用户需要独立于任何其他应用程序表单与Y进行交互。

在x中,您可以将Y.topmest=true;在Z.ShowDialog()之后。这将使y处于首位。然后,如果您想让其他表单工作,可以输入y.TopMost=false;就在y之后。最顶端=真;这将把窗口放在顶部,但允许其他窗体稍后再查看它


或者,如果问题是一个表单被置于另一个表单之上,那么您可以更改表单属性中其中一个表单的开始位置。

您可以将主表单(X)更改为MDI容器表单(IsMdiContainer=true)。然后,您可以将其余表单作为子表单添加到X。然后使用Show方法而不是ShowDialog加载它们。这样,所有子窗体都将在容器中浮动

您可以将子窗体添加到X,如下所示:

ChildForm Y = new ChildForm();
Y.MdiParent = this //X is the parent form
Y.Show();

如果要使用
ShowDialog
显示窗口,但不希望它阻止主窗体以外的其他窗口,则可以在单独的线程中打开其他窗口。例如:

private void ShowY_Click(object sender, EventArgs e)
{
    //It doesn't block any form in main UI thread
    //If you also need it to be always on top, set y.TopMost=true;

    Task.Run(() =>
    {
        var y = new YForm();
        y.TopMost = true;
        y.ShowDialog();
    });
}

private void ShowZ_Click(object sender, EventArgs e)
{
    //It only blocks the forms of main UI thread

    var z = new ZForm();
    z.ShowDialog();
}