By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

Treeview - Identify top level node

P: n/a
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

Jul 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
xla76 <pe*********@gmail.comwrote in news:1183983720.731689.205710
@d55g2000hsg.googlegroups.com:
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

Jul 9 '07 #2

P: n/a
On Jul 9, 1:00 pm, Arthur Dzhelali <a.dzhel...@theday.comwrote:
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

xla76 <pete.for...@gmail.comwrote in news:1183983720.731689.205710
@d55g2000hsg.googlegroups.com:
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
' 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

Jul 9 '07 #3

P: n/a
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
Jul 9 '07 #4

P: n/a
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?

Jul 9 '07 #5

P: n/a
On Jul 9, 2:14 pm, xla76 <pete.for...@gmail.comwrote:
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

Jul 9 '07 #6

P: n/a
On Jul 9, 7:37 pm, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On Jul 9, 2:14 pm, xla76 <pete.for...@gmail.comwrote:
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

Jul 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.