Difference between .Owner property and ShowDialog(IWin32Window owner)?(.Owner 属性和 ShowDialog(IWin32Window 所有者)之间的区别?)
问题描述
我认为可以通过 .Owner 属性或通过在重载方法 ShowDialog(IWin32Window owner)
I presume a winform's owner can be set explicitly via the .Owner property OR by passing the owner in the overloaded method ShowDialog(IWin32Window owner)
我无法理解为什么这些方法在处理 MDI 表单时会表现出不同的行为.
I am unable to understand why these methods exhibit different behavior when working with MDI forms.
我创建了一个 MDIParent 和一个 MDIChild.
I have created an MDIParent and an MDIChild.
我还有一个简单的 winform MyDialogBox,它在加载时显示其所有者.
I also have a simple winform MyDialogBox that displays its owner on load.
MessageBox.Show("Dialog's owner is " + this.Owner.Name);
方法 A - 在加载 MDIChild 我有以下代码,这导致 MyDialogBox 的所有者被设置为 MDIChild
Method A - In the load of MDIChild I have the following code, which causes the MyDialogBox's owner to be set to MDIChild
MyDialogBox box = new MyDialogBox();
box.Owner = this; // Set owner as MDIChild
box.ShowDialog();
方法 B - 或者,在 MDIChild 的加载方法中,我有以下代码,这导致 MyDialogBox 的所有者被设置为 MDIParent
Method B - Alternatively, in the load method of MDIChild I have the following code, which causes the MyDialogBox's owner to be set to MDIParent
MyDialogBox box = new MyDialogBox();
box.ShowDialog(this); // Pass MyMDIChild as owner
我还阅读了以下这里
只有 MDI 父窗体可以拥有另一个窗体,无论是 MDI 子窗体、模式对话框还是父窗体设置为所有者参数的窗体.
如果是这样,方法 A 根本不应该工作,不是吗?
If so Method A should not work at all, isn't it ?
我错过了什么?为什么方法 B 不将所有者设置为 MDIChild ?
What am I missing? Why doesn't method B set the owner to MDIChild ?
推荐答案
看这2个选项使用Reflector的区别,似乎它们的实现略有不同:box.Owner = this
只需将提供的 this 值分配给内部所有者字段.但是,当调用 ShowDialog(IWin32Window)
时,实现会在分配值之前执行以下调用:
Looking at the differences of these 2 options using Reflector, it seems that they have a slightly different implementation:
box.Owner = this
just assign the provided value of this to the internal owner field.
However, when calling ShowDialog(IWin32Window)
, the implementation performs the following call, prior to assigning the value:
owner = ((Control) owner).TopLevelControlInternal;
这可能会导致分配 MDIParent.
This might result in assignment of the MDIParent.
(注意:我远不是 MDI 方面的专家,所以我在这里可能错了).
(Note: I'm far from being an expert regarding MDI, so I might be wrong here).
这篇关于.Owner 属性和 ShowDialog(IWin32Window 所有者)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.Owner 属性和 ShowDialog(IWin32Window 所有者)之间的区别?


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01