C# 如何使表格始终在应用程序的顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18453818/
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-10 12:14:57  来源:igfitidea点击:

How to make form always on top in Application

c#winforms

提问by

I have a form that i want to always be on top whenever it is opened in the application but i dont want it to be on top when the main form is minimized or another application is navigated. I want it to be on top only in my application.

我有一个表单,每当它在应用程序中打开时,我都希望它始终位于顶部,但我不希望它在主表单最小化或导航另一个应用程序时位于顶部。我希望它仅在我的应用程序中位于顶部。

Following the answer in the question : How to make a window always stay on top in .Net?

按照问题中的答案:如何使窗口始终保持在 .Net 的顶部?

    this.TopMost = true;

Makes the form on top but the form is still on top when another application is navigated to or the main form is closed.

使表单位于顶部,但当导航到另一个应用程序或关闭主表单时,该表单仍位于顶部。

Pls how do i make the form only on top in the application while enabling user to still work on the main form?

请问如何在应用程序的顶部制作表单,同时使用户仍然可以在主表单上工作?

采纳答案by Hans Passant

You are looking for an ownedwindow. It is always on top of the owner and it gets minimized along with the owner. Good examples of owned windows are the various helper windows inside Visual Studio. You can undock them but they'll always stay on top of the VS main window.

您正在寻找拥有的窗户。它始终位于所有者之上,并且与所有者一起最小化。自有窗口的好例子是 Visual Studio 中的各种帮助窗口。您可以取消停靠它们,但它们将始终停留在 VS 主窗口的顶部。

You create an owned window by displaying it with the Show(owner) overload. Or by explicitly assigning its Owner property.

您可以通过使用 Show(owner) 重载来显示它来创建一个拥有的窗口。或者通过显式分配其 Owner 属性。

回答by Sorceri

Set the top level and then set the owner, example below.

设置顶层,然后设置所有者,示例如下。

public Form1()
{
    InitializeComponent();
    Form2 f2 = new Form2();
    //top level not really needed
    f2.TopLevel = true;
    f2.Show(this);
}