I need to populate a context menu from a database at run time. I do not know the number of items that will be in the list, so I would like to handle the click event in a single place. How do I declare the handler so I can tell which menu item actually triggered the click.
Public Function GetBookmarkContextMenu(ByVal aBookmark As Bookmark) As System.Windows.Controls.ContextMenu Dim myContextMenu As New Controls.ContextMenu myContextMenu.Name = 'BookmarkMenu' For Each aMailingList As MasterService.FalconBookmarkMailingListType In GlobalUserSettings.MailingLists Dim mySubMenuItem As New Controls.MenuItem mySubMenuItem.Name = 'MailingListName' & aMailingList.ID.ToString mySubMenuItem.Header = aMailingList.Title AddHandler (myMenuItem.Click), AddressOf ForwardToList_Click mySubMenuItem.IsEnabled = True myMenuItem.Items.Add(mySubMenuItem) Next myContextMenu.Items.Add(myMenuItem) return myContextMenu End Function Public Sub ForwardToList_Click() 'How do I know which of the dynamically created items was clicked? End Sub
Your ForwardToList_Click() should include parameters for the sender and event args:
‘sender’ is the control that caused the event, which is what I believe you’re looking for.