.Owner 属性和 ShowDialog(IWin32Window 所有者)之间的区别? [英] Difference between .Owner property and ShowDialog(IWin32Window owner)?

查看:52
本文介绍了.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 所有者)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆