Solved

TreeView Control in C#

Posted on 2006-06-30
1
250 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
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

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

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

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

23 Experts available now in Live!

Get 1:1 Help Now