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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.