在运行时将MdiChild ToolStrip显示为MdiParent。 [英] Displaying MdiChild ToolStrip into MdiParent during runtime.

查看:62
本文介绍了在运行时将MdiChild ToolStrip显示为MdiParent。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含MdiParent表单和少量MdiChild表单的MDI应用程序。 MdiParent有一个固定的MenuStrip,一个固定的ToolStrip和一个固定的StatusStrip。 MdiChild表单包含一个或多个ToolStrips和一个StatusStrip以及其中的其他控件。 ToolDtrips和MdiChild的StatusStrip通常是隐藏的,仅在MdiParent中打开表单时显示。基于这种情况,我有以下目标:



1)当在父项中打开MdiChild时,其ToolStrip应与固定菜单一起显示和父级的ToolStrip。请注意,我不是在谈论将子表单的ToolStrip与父表单的ToolStrip合并。它应该显示子项的ToolStrip低于父项的固定项。但是,孩子的StatusStrip应该像往常一样与父母的状态合并。



2)当MdiChild关闭时,其ToolStrip应该从MdiParent。 StatusStrip也应该从父级的名称中取消合并。



3)由于可以打开多个Mdi孩子,当前活动的MdiChild的ToolStrip应该是在MdiParent中显示,失去焦点的孩子应该像往常一样被删除。同样的规则也适用于StatusStrips。



我试图做的是,在MdiParent中取一个ToolStripPanel并将固定的Menu和ToolStrip放入其中。然后,当MdiChild打开或获得焦点时,将其工具条添加到父项的ToolStripPanel中。稍后当表单失去焦点或关闭时,从父级的ToolStripPanel中删除其工具条。



StatusStrips的处理通常由Merge完成子窗体打开/获得焦点并关闭/失去焦点时,ToolStripManager类的RevertMerge和RevertMerge方法。



其中一个子窗体的相关代码示例如下:



 私人  Sub  CustomerList_Activated( ByVal  sender 作为 对象 ByVal  e  As  System.EventArgs) Handles  。激活
如果 MdiParent Nothing 然后
Dim parent As Home = MdiParent
Dim lastIndex As 整数 = Array.IndexOf(parent.ToolStripPanel1.Rows,parent.ToolStripPanel1.Rows.Last)
parent.ToolStripPanel1.Join( .ToolStrip1,lastIndex + 1

.ToolStrip1.Visible = True
ToolStripManager.Merge( Me .StatusStrip1, CType Me .MdiParent,Home).StatusStrip1)
结束 如果
En d Sub





 私有  Sub  CustomerList_Deactivate( ByVal  sender < span class =code-keyword>作为 对象 ByVal  e  As  System.EventArgs)句柄  .Deactivate 
如果 MdiParent Nothing 然后
Dim parent As Home = MdiParent
Dim lastIndex As 整数 = Array.IndexOf (parent.ToolStripPanel1.Rows,parent.ToolStripPanel1.Rows.Last)
Array.Clear(parent.ToolStripPanel1.Rows,lastIndex, 1
parent.ToolStripPanel1.Controls.RemoveAt(lastIndex)

ToolStripManager.RevertMerge( CType Me .MdiParent,Home).StatusStrip1, Me .StatusStrip1)
结束 < span class =code-keyword>如果
结束 Sub





但问题是,每当我尝试关闭MdiChild时,都会抛出一个ObjectDisposedException,指出无法访问已处置的对象。对象名称:'Icon'。遍历各种MdiChild表单时不会出现此问题。关闭整个应用程序时也不会出现这种情况。



另外,我找不到从ToolStripPanel取消连接项目的方法。



请帮忙解决这个问题。还要提一下是否有更好的方法来实现上述三个目标。



随意问我是否能说清楚。



问候!

解决方案

这个想法是:谁需要MDI?为什么要折磨自己并吓跑你的用户?

帮自己一个大忙:根本不要使用MDI。没有它,您可以更轻松地实现设计,质量更好。 MDI甚至被微软高度劝阻,事实上,微软将其从WPF中删除并且很难支持它。更重要的是,如果您使用MDI,您将吓跑所有用户。只是不要。请参阅:

http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [ ^ ],

如何在WPF中创建MDI父窗口? [ ^ ]。



我可以解释做什么。请看我过去的答案:

如何在WPF中创建MDI父窗口? [解决方案2 ],

关于在WPF中使用MDI窗口的问题 [ ^ ],

麦当劳给出错误 [ ^ ],

如何设置子窗体最大化,最后一个子窗体最小化 [ ^ ]。



-SA

I have an MDI application containing an MdiParent form and few MdiChild forms. The MdiParent has a fixed MenuStrip, a fixed ToolStrip and a fixed StatusStrip. The MdiChild forms have one or more ToolStrips and a StatusStrip along with other controls in them. The ToolStrips and the StatusStrip of the MdiChild are generally hidden and are only shown when the form is opened in the MdiParent. Based on this scenario I have the following objectives:

1) When an MdiChild is opened in the parent, its ToolStrip(s) should be shown along with the fixed Menu and ToolStrip of the parent. Please note, I ‘m not talking about merging the ToolStrip of the child form to that of the parent. It should show the ToolStrip of the child below the fixed ones of the parent. However, the StatusStrip of the child should merge with that of the parent as usual.

2) When an MdiChild is closed, its ToolStrip(s) should be removed from the MdiParent. The StatusStrip should also be unmerged from that of the parent.

3) As multiple Mdi children can be opened, the ToolStrip(s) of the currently active MdiChild should be shown in the MdiParent and those of the child losing focus should be removed as usual. The same rule is applicable for the StatusStrips also.

What I tried to do is, take a ToolStripPanel in the MdiParent and place the fixed Menu and ToolStrip in it. Then, when the MdiChild is opened or gains focus, add its toolstrip(s) into the ToolStripPanel of the parent. Later when the form loses focus or is closed, remove its toolstrip(s) from the ToolStripPanel of the parent.

The handling of the StatusStrips is typically done by the Merge and RevertMerge methods of ToolStripManager class when the child form opens/gains focus and closes/loses focus respectively.

The relevant code example for one of the child forms follows:

Private Sub CustomerList_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If Not MdiParent Is Nothing Then
            Dim parent As Home = MdiParent
            Dim lastIndex As Integer = Array.IndexOf(parent.ToolStripPanel1.Rows, parent.ToolStripPanel1.Rows.Last)
            parent.ToolStripPanel1.Join(Me.ToolStrip1, lastIndex + 1)

            Me.ToolStrip1.Visible = True
            ToolStripManager.Merge(Me.StatusStrip1, CType(Me.MdiParent, Home).StatusStrip1)
        End If
    End Sub



Private Sub CustomerList_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
        If Not MdiParent Is Nothing Then
            Dim parent As Home = MdiParent
            Dim lastIndex As Integer = Array.IndexOf(parent.ToolStripPanel1.Rows, parent.ToolStripPanel1.Rows.Last)
            Array.Clear(parent.ToolStripPanel1.Rows, lastIndex, 1)
            parent.ToolStripPanel1.Controls.RemoveAt(lastIndex)

            ToolStripManager.RevertMerge(CType(Me.MdiParent, Home).StatusStrip1, Me.StatusStrip1)
        End If
    End Sub



But the problem is, every time I try to close an MdiChild an ObjectDisposedException is thrown stating "Cannot access a disposed object. Object name: ‘Icon’". The problem doesn’t arise while traversing through the various MdiChild forms. It doesn’t arise while closing the whole application also.

Also, I couldn’t find a way to unjoin items from a ToolStripPanel.

Please help in solving this problem. Also mention if there’s some better way to achieve the above three objectives.

Feel free to ask if I couldn’t make myself clear.

Regards!

解决方案

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


这篇关于在运行时将MdiChild ToolStrip显示为MdiParent。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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