By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

Displaying MdiChild ToolStrip into MdiParent during runtime.

P: 79
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerList_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
  2.         If Not MdiParent Is Nothing Then
  3.             Dim parent As Home = MdiParent
  4.             Dim lastIndex As Integer = Array.IndexOf(parent.ToolStripPanel1.Rows, parent.ToolStripPanel1.Rows.Last)
  5.             parent.ToolStripPanel1.Join(Me.ToolStrip1, lastIndex + 1)
  7.             Me.ToolStrip1.Visible = True
  8.             ToolStripManager.Merge(Me.StatusStrip1, CType(Me.MdiParent, Home).StatusStrip1)
  9.         End If
  10. End Sub

Expand|Select|Wrap|Line Numbers
  1. Private Sub CustomerList_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
  2.         If Not MdiParent Is Nothing Then
  3.             Dim parent As Home = MdiParent
  4.             Dim lastIndex As Integer = Array.IndexOf(parent.ToolStripPanel1.Rows, parent.ToolStripPanel1.Rows.Last)
  5.             Array.Clear(parent.ToolStripPanel1.Rows, lastIndex, 1)
  6.             parent.ToolStripPanel1.Controls.RemoveAt(lastIndex)
  8.             ToolStripManager.RevertMerge(CType(Me.MdiParent, Home).StatusStrip1, Me.StatusStrip1)
  9.         End If
  10. 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.

Oct 28 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.