We help IT Professionals succeed at work.

find the treeview node level with a click on the node in vb. net

StateGuy
StateGuy asked
on
I want to return a treeview node level with a single mouse click (1, 2 or 3).  I'm loading a treeview from a database (which works just peachy) and need to know what level I've clicked.  There are up to three levels: parent, child, grandchild.  What I want to do is right click on a node to get a specific context menu to deal with that node at that level.  Each level will have it's own context menu.

This works to a point.  But I get the level down one from the node I click.  In orther words, if I click the parent node, I get the child node text.
    Private Sub TreeView1_MouseDown(ByVal sender As Object, _
                    ByVal e As System.Windows.Forms.MouseEventArgs) _
                    Handles TreeView1.MouseDown
        If e.Button = MouseButtons.Right Then
            Dim n As TreeNode = TreeView1.GetNodeAt(e.X, e.Y)
            mnuContextLevel1.Show(TreeView1, New Point(e.X, e.Y))
            MsgBox(TreeView1.SelectedNode.Nodes(TreeView1.SelectedNode.Level).ToString)
        End If
    End Sub

I'm kinda hoping it's a simple solution since I'm running out of points.  If I'm not clear enough, please let me know.

Randy
Comment
Watch Question

Why not simply use the level?





MsgBox(TreeView1.SelectedNode.Level).ToString)

Open in new window

Author

Commented:
I got an error message:
"Overload resolution failed because no accessible 'ToString' can be called with these arguments."

My bad.


  	
 
MsgBox(TreeView1.SelectedNode.Level.ToString)

Open in new window

Author

Commented:
OO-RAH!  And don't worry about the misplaced paren.  I made a mistake once myself...
<humor inetnded>
Thanks again.

Randy