Treeview - Identify top level node

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
 
A

Arthur Dzhelali

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
 
R

rowe_newsgroups

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
 
H

Heikki Leivo

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
 
X

xla76

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?
 
R

rowe_newsgroups

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
 
X

xla76

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top