Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView ContextMenu only on certain nodes 1

Status
Not open for further replies.

jebenson

Technical User
Feb 4, 2002
2,956
US
Hello all,

I am trying to make a ContextMenu appear on right-click in a TreeView, but only when the user right-clicks on a certain "level" of nodes. I actually have this working. The problem is that the ContextMenu does not disappear on the first click when the TreeView is clicked somewhere else after the ContextMenu appears. The menu disappears on a second click, but I need for it to disappear on the first (i.e., normal program behavior with which we are all familiar).

Here's my code so far.

Code:
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
    Dim dp As System.Drawing.Point

    TreeView1.ContextMenu = Nothing

    If e.Button = MouseButtons.Right Then

        'select node that is being right-clicked
        TreeView1.SelectedNode = TreeView1.GetNodeAt(e.X, e.Y)

        'Get the nesting level of the selected node
        NodeLevel = GetNodeNestingLevel(TreeView1.SelectedNode)

        'only display context menu on lowest-level nodes
        If NodeLevel = 3 Then
            TreeView1.ContextMenu = TVMenu
            dp = New System.Drawing.Point(e.X, e.Y)
            TreeView1.ContextMenu.Show(TreeView1, dp)
        Else
            TreeView1.ContextMenu = Nothing
        End If
    ElseIf e.Button = MouseButtons.Left Then
        TreeView1.SelectedNode = TreeView1.GetNodeAt(e.X, e.Y)
        TreeView1.ContextMenu = Nothing
    End If

    NodeLevel = -1
End Sub

Don't worry about the GetNodeNestingLevel function - it is working properly.

Any ideas?


Thanks,
JEB

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
try making your ContextMenu dynamically in a sub as such:

Code:
sub MakeContextMenu(ByVal MenuType as String)

     select case MenuType
       case "Show"
         'make your menu here
         ContextMenu.MenuItems.Clear()                  
         ContextMenu1.MenuItems.Add("item name", _ 
              New System.EventHandler(AddressOf _   
              Me.itemFunction))

       case else
         'clear out the ContextMenu
         ContextMenu.MenuItems.Clear()

     end select

end sub

function itemFunction(ByVal sender As Object, _
        ByVal e As System.EventArgs)

     'make your function do something

end function


call MakeContextMenu("Show") before you assign the TreeView.ContextMenu

try that out, i used it and it works...


Bob
 
oops forgot to say this... put this in your Else

MakeContextMenu("Clear")
TreeView.ContextMenu = Nothing

"Clear" can be anything besides "Show" obviously...
and that ContextMenu1.MenuItems.Add... was a mistake, i cut and pasted it... should have been ContextMenu.MenuItems.Add...


Sorry bout that...

Bob
 
I made some changes to your code and It seems to work the way you want it to. I am just changing the context menu and letting the treeview do the rest.

Code:
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
    Dim dp As System.Drawing.Point

    If e.Button = MouseButtons.Right Then

        'select node that is being right-clicked
        TreeView1.SelectedNode = TreeView1.GetNodeAt(e.X, e.Y)

        'Get the nesting level of the selected node
        NodeLevel = GetNodeNestingLevel(TreeView1.SelectedNode)

        'only display context menu on lowest-level nodes
        If NodeLevel = 3 Then
            TreeView1.ContextMenu = TVMenu
        Else
            TreeView1.ContextMenu = Nothing
        End If
    End If

    NodeLevel = -1
End Sub
 
Thanks buddy! It works just fine now. Have a star!

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
kindly tell me that how can I get the index of the selected node in the treeview control in vb.net

give me code of GetNodeNestingLevel()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top