views:

725

answers:

2

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.

Thanks,

Dave

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

Your ForwardToList_Click() should include parameters for the sender and event args:

Public Sub ForwardToList_Click(sender As Object, e As EventArgs)
'...
End Sub

"sender" is the control that caused the event, which is what I believe you're looking for.

Jon B
A: 

Works, thanks!

Best to add a comment to the answer you like, and then upvote and/or accept the answer. :)
AR