Treeview - Identify top level node

  • Thread starter Thread starter xla76
  • Start date Start date
X

xla76

I have a simple treeview (treeview1) to which I have added two nodes
(nodeA and nodeB) which have n levels of child nodes.

What I want is to be able to identify whether the child node I select
is under NodeA or NodeB. I can do this by splitting the
selectednode.fullpath of the child node but there must be a better
way.

Can any kind soul can enlighten me?

Cheers
Pete
 
If I got your question right.
You need to find Parent node
here you go:

dim pNode as TreeNode
if SelectedNode.Parent isNot Nothing then
pNode=SelectedNode.Parent
' Now once you got your parent node you can use tag/text/name properties
' or any other means to determine if this is NodeA or NodeB

end if
 
If I got your question right.
You need to find Parent node
here you go:

dim pNode as TreeNode
if SelectedNode.Parent isNot Nothing then
pNode=SelectedNode.Parent
' Now once you got your parent node you can use tag/text/name properties
' or any other means to determine if this is NodeA or NodeB

end if





' Now once you got your parent node you can use tag/text/name properties
' or any other means to determine if this is NodeA or NodeB

Or just use "if pNode is NodeA then..."

Thanks,

Seth Rowe
 
dim pNode as TreeNode
if SelectedNode.Parent isNot Nothing then
pNode=SelectedNode.Parent
' Now once you got your parent node you can use tag/text/name properties
' or any other means to determine if this is NodeA or NodeB

end if

Or maybe even better:

Function IsChildNode(ChildNode as TreeNode, ParentCandidate as TreeNode) As
Boolean
Dim parentNode As TreeNode = ChildNode.Parent
Do Until parentNode Is Nothing
If parentNode Is ParentCandidate Then Return True
'Or: If parentNode.Equals(ParentCandidate) Then Return True
parentNode = parentNode.Parent
Loop
Return False
End Function
 
Still not working, I always get nodeA as the result, even when
selecting a nested child node of nodeB.

' Created as follows...

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?
 
Still not working, I always get nodeA as the result, even when
selecting a nested child node of nodeB.

' Created as follows...

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
 
Still not working, I always get nodeA as the result, even when
selecting a nested child node of nodeB.
' Created as follows...
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

Thanks Seth (and everyone else) - works a treat
 
Back
Top