Solved

C# TreeView Question

Posted on 2006-06-19
1
1,095 Views
Last Modified: 2012-08-13
How to traverse a TreeView in C# Windows Form and list all the leaf nodes in the tree?
0
Comment
Question by:michellechan57
1 Comment
 
LVL 4

Accepted Solution

by:
fffej78 earned 125 total points
ID: 16933034
TreeView.Nodes gives you a collection of TreeNode objects.  A TreeNode object has a number of children (Nodes property).  If if has no nodes, it is a child.

Let's write some recursive code to do this:

If we visit a tree node and the child nodes are zero in size, we have a leaf.  In this case we print out some representation of the tree node.  Otherwise we visit each of the child nodes in turn.

// We start things off like this in whatever method you want, like traverse.
foreach (TreeNode treeNode in treeView1.Nodes)
{
  visit( treeNode );
}

We then define a visitor type pattern. [see http://en.wikipedia.org/wiki/Visitor_pattern ]

private void visit( TreeNode treeNode )
{
  if (treeNode.Nodes.Count == 0)
  {
    // If we want to make this more general, we might have a processor interface passed into this
    // and do something like processor.accept( treeNode ), but here we'll just print it out :)
    System.Console.WriteLine(treeNode.ToString());
  }
  else
  {
    foreach (TreeNode t in treeNode.Nodes)
    {
      visit(t);
    }
  }
}

Obviously it'd be trivial to adapt this to return a list of leaf nodes.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 83
Hey Audio! 3 30
.NET 2008 VB and C# 6 28
Create a WebAPI using a generic return parameter 9 25
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question