每次右键单击上下文菜单 vb.net

context menu with every right click vb.net

我正在尝试将上下文菜单添加到 vb.net 中的树视图,但没有得到所需的行为。当我在树视图中右键单击时,我可以获得一个上下文菜单,根据用户单击的位置显示不同的选项,但是如果用户单击树视图中的其他位置并且上下文菜单已经出现,则上下文菜单会简单地清除并执行不会重新出现在新的点击位置(很像在网络浏览器中多次右键点击)。

这是我目前拥有的:

Private Sub treeviewMain_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles treeviewMain.MouseDown

    If e.Button = Windows.Forms.MouseButtons.Right Then
       'Determine which tree structure is loaded in tree view.
        If (treeviewMain.Nodes.Item(0).Name = "MainNode") Then
            Test(sender, e)
        Else
            OtherTest(sender, e)
        End If
    End If
End Sub

Private Sub Test(sender As Object, e As System.Windows.Forms.MouseEventArgs)

    Dim Context As ContextMenu = New ContextMenu
    Dim NodeClicked As TreeNode

    NodeClicked = Me.tvwMain.GetNodeAt(e.X, e.Y)

    If NodeClicked Is Nothing Then
        Context.MenuItems.Add("New Item")
        Context.Show(treeviewMain, e.Location)
    ElseIf NodeClicked.Name = "MainNode" Then
        tvwMain.SelectedNode = NodeClicked
        Context.MenuItems.Add("New Item")
        Context.Show(treeviewMain, e.Location)
    ElseIf NodeClicked.Level = 0 And NodeClicked.Name <> "MainNode" Then
        tvwMain.SelectedNode = NodeClicked
        Context.MenuItems.Add("Rename")
        Context.MenuItems.Add("Delete")
        Context.MenuItems.Add("-")
        Context.MenuItems.Add("New")
        Context.Show(treeviewMain, e.Location)
    ElseIf NodeClicked.Level = 1 Then
        tvwMain.SelectedNode = NodeClicked
        Context.MenuItems.Add("Edit")
        Context.MenuItems.Add("Copy")
        Context.MenuItems.Add("Delete")
        Context.Show(treeviewMain, e.Location)
    End If
End Sub

在 vb.net 中是否有这种行为?

我认为您不需要在 MouseDown 事件中编写任何代码。您只需要做的是 - 在填充 TreeView 时为每个 Node 正确设置 ContextMenu

如果我是你,我会这样做

    ContextMenuStrip1.Items.Add("New Item")

    ContextMenuStrip2.Items.Add("Rename")
    ContextMenuStrip2.Items.Add("Delete")
    ContextMenuStrip2.Items.Add("-")
    ContextMenuStrip2.Items.Add("New")

    ContextMenuStrip3.Items.Add("Edit")
    ContextMenuStrip3.Items.Add("Copy")
    ContextMenuStrip3.Items.Add("Delete")

    Dim mainNode As New TreeNode("Main Node")
    mainNode.Name = "MainNode"
    mainNode.ContextMenuStrip = ContextMenuStrip1

    Dim level0Node As New TreeNode("Other Level 0 Node")
    level0Node.ContextMenuStrip = ContextMenuStrip2

    Dim level1NodeOfMain As New TreeNode("Level 1 Node Under Main Node")
    level1NodeOfMain.ContextMenuStrip = ContextMenuStrip3

    Dim level1NodeOfOther As New TreeNode("Level 1 Node Under Other Level 0")
    level1NodeOfOther.ContextMenuStrip = ContextMenuStrip3

    mainNode.Nodes.Add(level1NodeOfMain)
    level0Node.Nodes.Add(level1NodeOfOther)

    treeviewMain.Nodes.Add(mainNode)
    treeviewMain.Nodes.Add(level0Node)

    treeviewMain.ContextMenuStrip = ContextMenuStrip1 'you can remove this line if you don't want to show ContextMenu if user clicks not on the Nodes