C# 如何使一种形式保持在另一种形式之上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1121209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:41:38  来源:igfitidea点击:

How do a make one form stay on top of another?

c#winforms

提问by BCS

I have found the Form.TopMostproperty but it puts the form on top of everything, including stuff that isn't part of my app. I've got a suspicion that I'm missing something obvious here. (Is Formthe proper bases class for a non modal dialog box?)

我找到了该Form.TopMost属性,但它将表单置于所有内容之上,包括不属于我的应用程序的内容。我怀疑我在这里遗漏了一些明显的东西。(Form是非模态对话框的正确基类吗?)

采纳答案by CodeFusionMobile

Use the Form.Owner property of your dialog form and set it to the main Form.

使用对话框窗体的 Form.Owner 属性并将其设置为主窗体。

Read more here http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx

在此处阅读更多信息 http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx

The Owned form will never be displayed behind the Owner Form.

拥有的表单永远不会显示在所有者表单的后面。

回答by Martin Liversage

You can specify parent-child relationships between windows by supplying the parent Form as parameter to the ShowDialog() method called on the child Form. The child window will then stay on top of the parent and also minimize and restore along with the parent.

您可以通过将父窗体作为参数提供给在子窗体上调用的 ShowDialog() 方法来指定窗口之间的父子关系。然后子窗口将停留在父窗口的顶部,并与父窗口一起最小化和恢复。

回答by EKS

If i understand you correctly your opening a form from your application, and you want your new form to be on top of the old one.

如果我理解您正确地从您的应用程序中打开一个表单,并且您希望您的新表单位于旧表单之上。

To do this you can use ShowDialog()and StartPosition

为此,您可以使用ShowDialog()StartPosition

SomeForm MyNewForm = new SomeForm();
MyNewForm.ShowDialog();

this will make that form stay on top of the orignal one, and you can also use

这将使该表格停留在原始表格的顶部,您也可以使用

MyNewForm .StartPosition = FormStartPosition.CenterParent;

To control where that new form shows on the screen.

控制新表单在屏幕上的显示位置。

回答by Ahmed Yossef

It is very simple; You just have to pass the owner when you call the Show()method

这很简单;你只需要在调用Show()方法时传递所有者

YourForm.Show(parentForm);