Solved

C# TreeView Question

Posted on 2006-06-19
1
1,084 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
Entity Framework Error: 'The user instance login flag is not supported' 12 32
Allow space in this pattern 2 49
Syntax error 9 45
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now