[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

TreeView Control in C#


Hi all,

I would like to know how to access all the selected nodes in a tree view control in C#. I have tree with some nodes. I select some of them by using control key, but I am having trouble in my c# program to get access to all the selected tree nodes. It seems like I can only select one  node at a time.

How do I get around this problem ??

Thank you for your help in advance.


Ramesh
0
Ramesh4u
Asked:
Ramesh4u
1 Solution
 
aaronfengCommented:
Yes, you are correct for Winform.  You can not retrieve all selected nodes at once, only one at a time.  You will have to traverse all the nodes in order to find all the selected nodes.  In the past, I wrapped the TreeView control, so I can add this functionality in one place; however, you can just create a function to do it for you.

You would do something like this:

private void FindAllCheckedNodes(List<TreeNode> outputNodes, TreeNodeCollection parentNodes) {
  foreach (TreeNode node in parentNodes) {
    if (node.SelectedNode) {
      outputNodes.Add(node);
    }
    FindAllCheckedNodes(outputNodes, node.Nodes);
  }
}

outputNodes will contain all the selected nodes when the function is done running.  You will pass in the root node as the second argument.

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now