vb.netwinformsmenumdi

MDI Child `MenuStrip` hidden on Non-Maximized and not visible anywhere


I have two WinForms. A frmMainMenu and a frmIndividual with frmIndividual a child of frmMainMenu. Each window has its own MenuStrip in the designer. However at runtime, the child menu does not appear to have a menu strip.

Here is the code creating the child. (It's literally the templated MDI script but modded.)

Private Sub IndividualToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles IndividualPlayerToolStripMenuItem.Click
    Dim ChildForm As New frmIndividual
    ' Make it a child of this MDI form before showing it.
    ChildForm.MdiParent = Me

    m_ChildFormNumber += 1
    ChildForm.Text = "(" & m_ChildFormNumber & ") " & ChildForm.Text

    ChildForm.Show()
End Sub

I doubled over the follow two questions/answers:

And I can confirm that I do now have a single menu, but the merging of menus happens even when the form isn't maximized.

What am I missing? Why are the menus merging when they weren't supposed to be?


Solution

  • This is the default behavior, the tool strip manager gets what you set to the merge-related properties in the MDI parent and child forms and strips then acts accordingly. The window state is not a merge factor. The Form.MainMenuStrip, ToolStrip.AllowMerge, ToolStripItem.MergeAction properties and strip's visibility are.

    Now, if you need to hide and merge the MenuStrip of the MdiChild Form only when you maximize it, then override its OnSizeChanged method to AllowMerge and merge the strips when so, otherwise, show the MenuStrip and revert the merge. Use the ToolStripManager class to manually merge and revert.

    ' MDI Child Form
    Protected Overrides Sub OnSizeChanged(e As EventArgs) 
        MyBase.OnSizeChanged(e)
    
        Dim isMax = WindowState = FormWindowState.Maximized
    
        menuStrip1.AllowMerge = isMax
        menuStrip1.Visible = Not isMax
    
        If MdiParent?.MainMenuStrip IsNot Nothing Then
            If isMax Then
                ToolStripManager.Merge(menuStrip1, MdiParent.MainMenuStrip)
            Else
                ToolStripManager.RevertMerge(MdiParent.MainMenuStrip)
            End If
        End If
    End Sub
    

    SOQ72981652