Dynamic Menu additions - how to select ?

mickle026

Member
Joined
Oct 31, 2009
Messages
20
Programming Experience
3-5
I know there is probably a simple solution to this but it is evading me, please help.

I have created a menu that alters according to mdichild windows open - see screenshots

As I hover over the menu it refreshes by adding or removing the child windows to/from the menu. My question is, what would I use as an "Item Clicked" in my code, because the subroutines are titled by their names? This Show menu item is ShowToolStripMenuItem. As i cannot create a subroutine based on an entry that is not there at design time, how is this done? - I know it has something to do with eventhandlers and AddressOf but I have never used them. An example would be fantastic.

VB.NET:
    Private Sub ShowToolStripMenuItem_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowToolStripMenuItem.MouseHover
        If ShowToolStripMenuItem.Text = "&Show" Then
            If ShowToolStripMenuItem.DropDownItems.Count = 2 Then
                ShowToolStripMenuItem.DropDownItems.Add("< No Image Windows >")
            End If
            ' remove previous forms from drop down (mdi children - do this so the list is always accurate)
            If ShowToolStripMenuItem.DropDownItems.Count > 2 And ShowToolStripMenuItem.DropDownItems.Item(2).Text <> "< No Image Windows >" Then
                For a = ShowToolStripMenuItem.DropDownItems.Count - 1 To 2 Step -1
                    ShowToolStripMenuItem.DropDownItems.RemoveAt(a)
                Next
            End If
            For Each frm As Form In Me.MdiChildren
                ' add image windows if they exist (mdi children)
                If ShowToolStripMenuItem.DropDownItems.Count > 2 And ShowToolStripMenuItem.DropDownItems.Item(2).Text = "< No Image Windows >" Then ShowToolStripMenuItem.DropDownItems.RemoveAt(2)
                ShowToolStripMenuItem.DropDownItems.Add(frm.Text)
            Next
            ' show the user they have no images open - this catches the exception when mdi child is closed and menu doesnt refresh
            If ShowToolStripMenuItem.DropDownItems.Count = 2 Then
                ShowToolStripMenuItem.DropDownItems.Add("< No Image Windows >")
            End If
        End If
    End Sub
dynamic menu 1.png

dynamic menu 2.jpg

Examples I have checked out all use a context menu strip,
VB.NET:
ShowToolStripMenuItem.DropDownItems.Add(frm.Text, New System.EventHandler(AddressOf OpenWindowAddress_Click)) <---------------------- Cannot do this here because compiler expects an image


    Private Sub OpenWindowAddress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' add event handler code
    End Sub


Any help will be greatly received, thanks.
 
Last edited:
I have finally figured out how to add event handlers to dropdown items in the main menu, however I still need to know or figure out if they are being removed properly.

Can anyone clarify how to list them?

Here is the code incase anyone else has had or is having this problem
VB.NET:
    Private Sub ShowToolStripMenuItem_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowToolStripMenuItem.MouseHover
        Try
            If ShowToolStripMenuItem.Text = "&Show" Then
                ' remove event handlers
                For a = ShowToolStripMenuItem.DropDownItems.Count To 2 Step -1
                    RemoveHandler ShowToolStripMenuItem.DropDownItems.Item(ShowToolStripMenuItem.DropDownItems.Count - 1).Click, AddressOf OpenWindowAddress_Click
                Next
                ' remove previous forms from drop down (mdi children - do this so the list is always accurate)
                If ShowToolStripMenuItem.DropDownItems.Count > 2 And ShowToolStripMenuItem.DropDownItems.Item(2).Text <> "< No Image Windows >" Then
                    For a = ShowToolStripMenuItem.DropDownItems.Count - 1 To 2 Step -1
                        ShowToolStripMenuItem.DropDownItems.RemoveAt(a)
                    Next
                End If
                ' show the user they have no images open
                If ShowToolStripMenuItem.DropDownItems.Count = 2 Then
                    ShowToolStripMenuItem.DropDownItems.Add("< No Image Windows >")
                End If
                ' add image windows if they exist (mdi children)
                For Each frm As Form In Me.MdiChildren
                    If ShowToolStripMenuItem.DropDownItems.Count > 2 And ShowToolStripMenuItem.DropDownItems.Item(2).Text = "< No Image Windows >" Then ShowToolStripMenuItem.DropDownItems.RemoveAt(2)
                    ShowToolStripMenuItem.DropDownItems.Add(frm.Text) ', New System.EventHandler(AddressOf OpenWindowAddress_Click))
                    AddHandler ShowToolStripMenuItem.DropDownItems.Item(ShowToolStripMenuItem.DropDownItems.Count - 1).Click, AddressOf OpenWindowAddress_Click
                Next
            End If
        Catch ex As Exception
            Debug.Print(ex.ToString)
        End Try
    End Sub

and here is the routine that catches all the clicks on that dynamic menu

VB.NET:
    Private Sub OpenWindowAddress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' add event handler code
        MessageBox.Show(sender.ToString, "Event Handler has Fired, this is the item that called it!")
    End Sub

event handler 1.png

event handler 2.png
 
Last edited:
What you're doing is a complete waste of time I'm afraid. The MenuStrip already includes functionality to automatically display a menu item for each MDI child window. You simply assign your Window menu item to the MdiWindowListItem property of the MenuStrip and each time you click that menu item it will display a drop-down item for each child window. No code required at all.

Besides that, the ToolStripmenuItem class has a constructor that allows you to specify the Click event handler and there is also an Add method that supports it too, e.g.
Me.WindowToolStripMenuItem.DropDownItems.Add("Child Window 1", Nothing, AddressOf MenuItem_Click)
 
Thanks for the explanation. I will certainly check it out. It might be a waste for this particular function but certainly not a waste to learn it.
Many thanks for your help guys :)

Sent using Tapatalk 2
 
Back
Top