.Owner 属性和 ShowDialog(IWin32Window 所有者)之间的区别?

Difference between .Owner property and ShowDialog(IWin32Window owner)?(.Owner 属性和 ShowDialog(IWin32Window 所有者)之间的区别?)
本文介绍了.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 所有者)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)