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.

Code:
    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,
Code:
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:

mickle026

Member
Joined
Oct 31, 2009
Messages
20
Programming Experience
3-5
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
Code:
    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

Code:
    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:

Herman

Well-known member
Joined
Oct 18, 2011
Messages
882
Location
Montreal, QC, CA
Programming Experience
10+
It's all pretty easy, when you add the item, you also AddHandler. When you remove the item you RemoveHandler.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,253
Location
Sydney, Australia
Programming Experience
10+
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)
 

mickle026

Member
Joined
Oct 31, 2009
Messages
20
Programming Experience
3-5
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
 
Top Bottom