Solved

TreeView Control in C#

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

708 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

13 Experts available now in Live!

Get 1:1 Help Now