×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

TreeView ContextMenu only on certain nodes

TreeView ContextMenu only on certain nodes

TreeView ContextMenu only on certain nodes

(OP)
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

RE: TreeView ContextMenu only on certain nodes

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

RE: TreeView ContextMenu only on certain nodes

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

RE: TreeView ContextMenu only on certain nodes

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

RE: TreeView ContextMenu only on certain nodes

(OP)
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

RE: TreeView ContextMenu only on certain nodes

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()

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close