将处理程序添加到动态创建的上下文菜单

发布于 2024-07-09 15:24:07 字数 1036 浏览 6 评论 0原文

我需要在运行时从数据库填充上下文菜单。 我不知道列表中的项目数量,因此我想在一个地方处理单击事件。 如何声明处理程序,以便我可以知道哪个菜单项实际触发了单击。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷炫老祖宗 2024-07-16 15:24:07

无法添加评论,所以我将其放在这里。 AundyKarthick 的精彩回复轻松出发
我的结果是这样的:

首先在表单上创建一个 contextmenustrip 在本例中为 ContextMenuStrip1

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  NamesTableAdapter.Fill(DataSet.Names)
  For Each element In DataSet.Names
     Dim mnuitem As New ToolStripMenuItem
     mnuitem.Name = element.Item(1)
     mnuitem.Text = element.Item(1)
     AddHandler (mnuitem.Click), AddressOf ToolMenuItem_Click
     ContextMenuStrip1.Items.Add(mnuitem)
  Next
End Sub

Private Sub ToolMenuItem_Click(sender As Object, ByVal e As EventArgs) 
    textbox1.Text = sender.name
End Sub

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

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  NamesTableAdapter.Fill(DataSet.Names)
  For Each element In DataSet.Names
     Dim mnuitem As New ToolStripMenuItem
     mnuitem.Name = element.Item(1)
     mnuitem.Text = element.Item(1)
     AddHandler (mnuitem.Click), AddressOf ToolMenuItem_Click
     ContextMenuStrip1.Items.Add(mnuitem)
  Next
End Sub

Private Sub ToolMenuItem_Click(sender As Object, ByVal e As EventArgs) 
    textbox1.Text = sender.name
End Sub
愛上了 2024-07-16 15:24:07
Dim mnuitm As New ToolStripMenuItem
mnuitm.Name = name_cbk.Items(i)
mnuitm.Text = name_cbk.Items(i)
AddHandler (mnuitm.Click), AddressOf item_Click
menulist.Items.Add(mnuitm)
Dim mnuitm As New ToolStripMenuItem
mnuitm.Name = name_cbk.Items(i)
mnuitm.Text = name_cbk.Items(i)
AddHandler (mnuitm.Click), AddressOf item_Click
menulist.Items.Add(mnuitm)
一花一树开 2024-07-16 15:24:07

您的 ForwardToList_Click() 应包含发送者和事件参数的参数:

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

“发送者”是导致事件的控件,我相信这就是您正在寻找的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文