Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I've got a treeview on a form and it contains a few hundred nodes, many indented. Think of the explorer view for example. This statement:

int i = ExampleTreeView.GetNodeCount(true);

returns the count of all the nodes in my example treeview. I've tried searching for a one-liner but if the treeview can tell you how many nodes there are, is there a method that will return a "flattened" list-cum-array of those nodes, also as one-liner statement?

I've seen lots of examples that use recursion to build a flattened list of nodes but maybe there's something in the framework that'll do it for me? I'd like to think the framework might have something but if I have to do it with recursion, then so be it.

Does anyone know of something builtin to the framework that'll do that for me?
Posted

1 solution

There is nothing in the framework that will convert a hierarchical tree of nodes into a flat list: it could not be sure that it would be possible to distingiush dfferent elements:
Root
  branch1
    leaf1
    leaf2
  branch2
    leaf1
    leaf2  
A default flat view would have only four elements, two called "leaf1" and two called "leaf2".

You will have to recursively parse the tree.
 
Share this answer
 
Comments
Reiss 13-Jul-11 5:14am    
You may want to write it as an extension method and put in your framework library.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900