Home » Windows OSRSS

How to Get all Nodes (parent and Child ) in a Treeview

Hi ,

 How to navigate through all Nodes(including Child Nodes) in  a treeview

tvwOpt.Nodes is Returning only Parent nodes collection in a treeview

Is there any property or easy way to Get entire nodes Colletion?

In my treeview i am dynamically loading all nodes.in my treeview i am loading around 13 Parent Nodes and around 50 Child nodes under them

I want to Loop through all the nodes and set some properties for that.I know we can achive it like

Finding all child nodes in parent nodes and looping through them.But still searching for an effective solution this

 

Plz help.

 

 

 

9 Answers Found

 

Answer 1

There is no property  or method on the treeview  to get all nodes, you need to use recursion to loop  through all nodes  in the hierarchy (or starting from a certain subroot node) :

		private void button1_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < tree.Nodes.Count; i++)
			{
				browseTreeNodes(tree.Nodes[i], 0);
			}
		}

		private void browseTreeNodes(TreeNode subRoot, int level)
		{
			if (subRoot == null)
				return;

			// do what you need here (just print to output for the purpose of demonstration - this is where "level" is used)
			string nodeText = subRoot.Text.PadLeft(subRoot.Text.Length + level, '\t');
			Console.WriteLine(nodeText);
			
			// loop through the children
			for (int i = 0; i < subRoot.Nodes.Count; i++)
			{
				browseTreeNodes(subRoot.Nodes[i], level+1);
			}
		}
best regards,
Vladimir

 

Answer 2

Thanks Vladimir

But This particular code does not Match my requirement.

 

I need to get the collection  of all nodes  and loop  through it.

Some thing like

Dim oNode As TreeNode

For Each oNode in tvwOpt.Nodes
  'My Logic
Next

 Right now i am writing two loops One for looping  through parent  Nodes and Another for looping through child  Nodes to achive  the same functionality as VB

I tried to create TreeNodeColletion object and planned to add all Parent and Child nodes to it.

Unfortunately TreeNodeColletion is an Sealed Object :(

is there any way to create TreeNodeCollection object,so that i can add all parent and child nodes to it in a function and will return the TreeNodeCollection to my Function.

 

 

Answer 3

You should be able to run an extension method on the treenodecollection class:

 

public static TreeNode[] GetAllNodes(this TreeNodeCollection collection)

{

  // your code here...

}

 

Answer 4

Hi SUNIL,

There is an article of "How to: Iterate Through All Nodes of a Windows Forms TreeView Control  " on MSDN :http://msdn.microsoft.com/en-us/library/wwc698z7(VS.80).aspx

Hope it helps.

Best Regards,
Helen Zhou


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
 

Answer 5


Private Sub GetAllNodes(ByVal aTreeView As TreeView) Dim n As TreeNode For Each n In aTreeView.Nodes MessageBox.Show("Parent Node " & n.Text) Dim CNode As TreeNode For Each CNode In n.Nodes MessageBox.Show("Child Node " & CNode.Text) Next Next End Sub

Best Regards

Sachin Manugade.

 

Answer 6

Thanks Vladimir

But This particular code does not Match my requirement.

 

I need to get the collection  of all nodes  and loop  through it.

Some thing like

Dim oNode As TreeNode

ForEach oNode in tvwOpt.Nodes
 'My LogicNext

 Right now i am writing two loops One for looping  through parent  Nodes and Another for looping through child  Nodes to achive  the same functionality as VB

I tried to create TreeNodeColletion object and planned to add all Parent and Child nodes to it.

Unfortunately TreeNodeColletion is an Sealed Object :(

is there any way to create TreeNodeCollection object,so that i can add all parent and child nodes to it in a function and will return the TreeNodeCollection to my Function.

 

Hi,

generally speaking, you could create TreeNodeCollection object using reflection, since the constructor is internal, but I wouldn't recommend it. The TreeNodeCollection class is strongly coupled with TreeNode and TreeView  classes, and expects a TreeNode object which is the owner of the collection. It represents a hierarchy, and it looks like you only need a flat list to iterate through. Couldn't you just use a List<TreeNode> to hold all the nodes and return it to your function ?

Best regards,
Vladimir

 

Answer 7

Couldn't you just use a List<TreeNode> to hold all the nodes  and return it to your function ?  

 

 

Can you please help  me in this.

How to get this entire   collection

Right now i have a property  called NodesCollection in XTreeOpt Usercontrol

i am just returning  tvwOpt.Nodes which will return only parent  nodes.Where as in VB it is returning Entire nodes collection  :(

How to return the same entire collection through a function

 Code:

 ReadOnly Property NodesCollection() As TreeNodeCollection
    Get
      Return tvwOpt.Nodes
    End Get
  End Property
XtreeOpt is an usercontrol which i am using across my project.When ever i need Nodes collection i am just calling this NodesCollection
 

Answer 8

Well you have to use recursion to go through the whole hierarchy ... try this code:

Public Class XtreeOpt

	ReadOnly property  NodesCollection() As List(Of TreeNode)
		Get
			Return Me.GetAllNodes()
		End Get
	End Property

	'Public Function GetAllNodes() As List(Of TreeNode)
	Protected Function GetAllNodes() As List(Of TreeNode)

		Dim allNodes As List(Of TreeNode) = New List(Of TreeNode)()

		' start recursion for each root node of the treeview
		For i As Integer = 0 To tvwOpt.Nodes.Count - 1
			GetAllNodes(tvwOpt.Nodes(i), allNodes)
		Next

		Return allNodes

	End Function

	Protected Sub GetAllNodes(ByVal subRoot As TreeNode, ByVal allNodes As List(Of TreeNode))

		' check for null (this can be removed since within th
		If (subRoot Is Nothing) Then
			Exit Sub
		End If

		' add subroot
		allNodes.Add(subRoot)
		' add all it's children
		For i As Integer = 0 To subRoot.Nodes.Count - 1
			GetAllNodes(subRoot.Nodes(i), allNodes)
		Next


	End Sub

End Class

I would personally loose the property and instead declare a public function :

Public Function GetAllNodes() As List(Of TreeNode)

to indicate to the caller that this is not a simple property, returning  an existing collection, but a function that creates a new collection  each time it's called. 

 

Best regards,
Vladimir

 

Answer 9

Thanks Alot....

its working fine

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter