473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Treeview - Identify top level node

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
6 11917
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Maileen | last post by:
Hi, I have on my form a treeview control with several nodes. when my form is loaded, I would like to preselected the 1st node of my treeview and to see it selected (highlighten node text) how...
1
by: bill | last post by:
When PopulateOnDemand is true, the treeview event handler for the TreeNodePopulate event gets the TreeNodeEventArgs as a parameter. The TreeNodeEventArgs contains the tree node to be populated,...
2
by: Pucca | last post by:
How do I make the TreeView control's node text (or label) editable when user click twice on it? (Not double click). It's like the explorer on our computer drives. The directory tree label...
0
by: saravanaVijayakumar | last post by:
I'm new to xml .. I had created a application such a way that I have to display the xml file in treeview control in C#.Net Application ... If I select the particular Node it should display the...
1
by: Bllich | last post by:
can anyone post an algoritam for the deepest level of a treeView ? it should be a recursive function I think.. I don't need treeView.Nodes.Level because my users don't select the nodes... I...
0
by: =?Utf-8?B?VmlrcmFt?= | last post by:
Hi, I am using Asp.net 2.0 treeview control.Tree is having 2-3 level of hirearechy. I want to show current selected node as expanded when the page reloads or postback. ow can I do that? Thanks
1
by: Roseo | last post by:
I have an xml file of the format below, node at level is 1 has to be <product>. Node at level 2 has to be <release> and node at level 3 has to be <change>. XSL has to verify that all nodes at level...
1
by: Falcula | last post by:
Hello, I have a treeview control, when i select a item i navigate to url. But selected node is lost, it reset itself, loosing state. I post my code here. Thanks in advance. <script...
2
by: michelqa | last post by:
Hi, I'm trying to add nodes to a specific node of a treeview. With VS 2005 and more (>= .net 2.0) I can set the Nodes.Name property and then use Nodes.Find("MyNodeName",true) to add nodes to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.