473,706 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.fu llpath 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 11964
If I got your question right.
You need to find Parent node
here you go:

dim pNode as TreeNode
if SelectedNode.Pa rent isNot Nothing then
pNode=SelectedN ode.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*********@gm ail.comwrote in news:1183983720 .731689.205710
@d55g2000hsg.go oglegroups.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.fu llpath 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...@the day.comwrote:
If I got your question right.
You need to find Parent node
here you go:

dim pNode as TreeNode
if SelectedNode.Pa rent isNot Nothing then
pNode=SelectedN ode.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...@gm ail.comwrote in news:1183983720 .731689.205710
@d55g2000hsg.go oglegroups.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.fu llpath 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.Pa rent isNot Nothing then
pNode=SelectedN ode.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(Chi ldNode as TreeNode, ParentCandidate as TreeNode) As
Boolean
Dim parentNode As TreeNode = ChildNode.Paren t
Do Until parentNode Is Nothing
If parentNode Is ParentCandidate Then Return True
'Or: If parentNode.Equa ls(ParentCandid ate) Then Return True
parentNode = parentNode.Pare nt
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("myNod eA")
Dim NodeB As TreeNode
NodeB = New TreeNode("myNod eB")
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...@gm ail.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("myNod eA")
Dim NodeB As TreeNode
NodeB = New TreeNode("myNod eB")
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.Expand All()
AddHandler treeView.NodeMo useClick, AddressOf
treeview_NodeMo useClick
Me.Controls.Add (treeView)

End Sub

Private Sub treeview_NodeMo useClick(ByVal sender As Object, ByVal
e As TreeNodeMouseCl ickEventArgs)
Dim node As TreeNode = DirectCast(send er,
TreeView).Selec tedNode

If node.Parent Is Nothing Then
MsgBox(String.F ormat("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.F ormat("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...@yah oo.comwrote:
On Jul 9, 2:14 pm, xla76 <pete.for...@gm ail.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("myNod eA")
Dim NodeB As TreeNode
NodeB = New TreeNode("myNod eB")
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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.Expand All()
AddHandler treeView.NodeMo useClick, AddressOf
treeview_NodeMo useClick
Me.Controls.Add (treeView)

End Sub

Private Sub treeview_NodeMo useClick(ByVal sender As Object, ByVal
e As TreeNodeMouseCl ickEventArgs)
Dim node As TreeNode = DirectCast(send er,
TreeView).Selec tedNode

If node.Parent Is Nothing Then
MsgBox(String.F ormat("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.F ormat("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
1139
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 can i do this please ? thanks, Maileen
1
2058
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, and child nodes can be added to it. Why is the Parent of the node contained in TreeNodeEventArgs always equal to nothing? I'm not talking about the root node, but about nodes which have been added
2
3262
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 becomes a selected sqaure where user can edit the labe. Thanks -- Thanks.
0
2188
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 attributes of that node in DataGrid But I am displaying the attributes in dataGrid if i use the following Code XmlNode x = root.SelectSingleNode("//JDF")
1
5093
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 need to get a random node from the whole collection so I want my rndLevel = random.next(deepest level) rndNode = random.next(number of nodes on that level) to be able to get any node on any level in the collection.
0
1126
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
1611
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 2 is <release>. If node at level 2 <change> , then XSL has to catch this error. Please let me know how it can be done in xsl. Toward the end of the following xml, I have a case of <change> appearing at level 2. Thanks in advance <product>...
1
3505
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 language="javascript">
2
1351
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 particular node of my treeview. ex :
0
8781
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9055
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8997
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7911
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2502
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2095
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.