Solved

TreeView Control in C#

Posted on 2006-06-30
1
268 Views
Last Modified: 2010-04-16

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
Comment
Question by:Ramesh4u
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
aaronfeng earned 50 total points
ID: 17022694
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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