c# - 从另一个表单显示一个表单

标签 c# winforms

当我想通过单击另一个窗体中的按钮来显示窗体 (C#) 时,我通常会从我想显示的窗体创建一个对象并使用 show 方法:

        Form2 f2 = new Form2();
        f2.Show();

或者我与“所有者”一起工作:

        Form2 tempForm = new Form2();
        this.AddOwnedForm(tempForm);
        tempForm.Show();

这两种方式产生相同的结果,但哪种方式最好,它们之间有什么区别?

最佳答案

除了命名之外唯一的区别是在第二个你调用AddOwnedForm ,而一开始你没有。查看我们看到的文档:

When a form is owned by another form, it is minimized and closed with the owner form. For example, if Form2 is owned by form Form1, if Form1 is closed or minimized, Form2 is also closed or minimized. Owned forms are also never displayed behind their owner form. You can use owned forms for windows such as find and replace windows, which should not be displayed behind the owner form when the owner form is selected.

因此,如果您希望表单的这种行为一起最小化,并且一个始终显示在另一个之上,请使用 AddOwnedForm。如果您不想要这种行为,请不要使用它。

关于c# - 从另一个表单显示一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2618830/

相关文章:

c# - 如何调用异步 Task<bool> 方法?

c# - 如何为所有文本框的特定目的使用通用子?

c# - Silverlight 编程绘图(从 Windows 窗体转换为 Silverlight)

c# - 以编程方式在 Crystal Report C# 中创建公式字段

c# - 如何在 ConfigureServices 方法中使用 ASP.NET Core 2.1 读取用户环境变量?

c# - IIS 错误 : Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/websiteName

c# - 打开 rdlc 报告时在报告查看器中手动设置值

c# - 在其他控件上方显示具有半透明背景颜色的标签?

c# - FlowLayoutPanel 自动调整大小

c# - 如何迭代/遍历 XML 文件元素(包含不同的命名空间)以将其转换为 C# 中的数据表