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

查看:902
本文介绍了.Owner财产和的ShowDialog(IWin32Window所有者)之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信一个winform的所有者可以明确地通过 .Owner 属性或通过传递主人在重载方法的ShowDialog(IWin32Window所有者)

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

MyDialogBox box = new MyDialogBox();
box.Owner = this; // Set owner as MDIChild
box.ShowDialog();



方法B - 或者,在MDIChild的加载方法我有下面的代码,这会导致MyDialogBox的业主被设置为的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 ?

推荐答案

综观使用反射这两个选项的差异,似乎他们有一个稍微不同的实现:
box.Owner =此刚分配的这内部所有者领域所提供的价值。
然而,当调用的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天全站免登陆