Still not working, I always get nodeA as the result, even when
selecting a nested child node of nodeB.
Dim NodeA As TreeNode
NodeA = New TreeNode("myNodeA")
Dim NodeB As TreeNode
NodeB = New TreeNode("myNodeB")
TreeView1.Nodes.Add(NodeA)
TreeView1.Nodes.Add(NodeB)
Have I done something wrong?
Here's a complete project that should work (note it will fire when you
click the [+] or [-] buttons giving the wrong node text). Just copy
this into a new form in your project and press F5 to run it.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim treeView As New TreeView()
Dim nodeA As New TreeNode("NodeA")
Dim nodeB As New TreeNode("NodeB")
For i As Integer = 1 To 5
nodeA.Nodes.Add(String.Format("NodeA child {0}", i))
nodeB.Nodes.Add(String.Format("NodeB child {0}", i))
Next
treeView.Nodes.Add(nodeA)
treeView.Nodes.Add(nodeB)
treeView.Dock = DockStyle.Fill
treeView.ExpandAll()
AddHandler treeView.NodeMouseClick, AddressOf
treeview_NodeMouseClick
Me.Controls.Add(treeView)
End Sub
Private Sub treeview_NodeMouseClick(ByVal sender As Object, ByVal
e As TreeNodeMouseClickEventArgs)
Dim node As TreeNode = DirectCast(sender,
TreeView).SelectedNode
If node.Parent Is Nothing Then
MsgBox(String.Format("You clicked a parent node named
{0}", node.Text))
Else
Dim parent As TreeNode = node.Parent
While Not parent.Parent Is Nothing
parent = parent.Parent
End While
MsgBox(String.Format("The node's parent is {0}",
parent.Text))
End If
End Sub
End Class
Hope that helps!
Thanks,
Seth Rowe