将处理程序添加到动态创建的上下文菜单
我需要在运行时从数据库填充上下文菜单。 我不知道列表中的项目数量,因此我想在一个地方处理单击事件。 如何声明处理程序,以便我可以知道哪个菜单项实际触发了单击。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无法添加评论,所以我将其放在这里。 AundyKarthick 的精彩回复轻松出发
我的结果是这样的:
首先在表单上创建一个 contextmenustrip 在本例中为 ContextMenuStrip1
Can't add a comment so I'll put it here. Excellent reply from AundyKarthick easily set out
my result was this:
First create a contextmenustrip on the form in this case ContextMenuStrip1
您的 ForwardToList_Click() 应包含发送者和事件参数的参数:
“发送者”是导致事件的控件,我相信这就是您正在寻找的。
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.