简体   繁体   English

.ShowDialog()无法显示MDI子窗口,为什么?

[英]MDI child window cannot be displayed by .ShowDialog() WHY?

I use winforms MDI window and display child windows on it. 我使用winforms MDI窗口并在其上显示子窗口。 I need to display some of them using modal dialog mode ( .ShowDialog() ) but still have them displayed as a child window (they should stay within the borders of the parent windows). 我需要使用模式对话框模式( .ShowDialog() )显示其中的一些,但仍将它们显示为子窗口(它们应位于父窗口的边界内)。

When I try to do this I get a compile time error saying that the child window cannot be displayed as a modal dialog mode window. 当我尝试执行此操作时,出现编译时错误,提示子窗口无法显示为模态对话框模式窗口。

Is there any way to do it? 有什么办法吗?

I don't think that there is direct way to do this. 我认为没有直接的方法可以做到这一点。 However, on the low level modal dialogs are implemented by disabling a parent window. 但是,在低级模式对话框是通过禁用父窗口来实现的。 Try to show modeless MDI child and keep parent disabled unless this child is closed. 尝试显示无模式MDI子级,并保持禁用父级,除非此子级已关闭。

I'd normally get around this by making all the controls on the childform be part of a user control and then add the user control to the childform and make a separate dialog also containing the user control. 通常,我会通过使子窗体上的所有控件成为用户控件的一部分来解决此问题,然后将用户控件添加到子窗体中,并创建一个也包含用户控件的单独对话框。 That means that you can reuse almost all the code and there's usually some code that will be different anyway (the childform normally doesn't have OK/Cancel buttons etc). 这意味着您可以重用几乎所有代码,并且通常会有一些代码会有所不同(子窗体通常没有OK / Cancel按钮等)。

However, if you don't want to go down that route here's a codeproject article that supposedly has a workaround (I've not looked at it carefully): MDI child as dialog form (MDI modal workaround) 但是,如果您不想沿这条路线走,这是一篇代码项目文章,据说它具有解决方法(我没有仔细研究过): MDI子级为对话框形式(MDI模态解决方法)

如果希望模式窗口仅在父表单的范围内打开,只需将表单属性StartPosition设置为CenterParent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM