1

How to get all checked items but to exclude main nodes with subitems?

For example take a look in this picture:

enter image description here

I want to get all checked items but to exclude marekd with yellow but still to have their subitems listed.

So far i did this:

List<String> CheckedNames(System.Windows.Forms.TreeNodeCollection theNodes)
    {
        List<String> aResult = new List<String>();

        if (theNodes != null)
        {
            foreach (System.Windows.Forms.TreeNode aNode in theNodes)
            {
                if (aNode.Checked)
                {
                    string[] itemName = Regex.Split(aNode.Text, " - ");
                    aResult.Add(itemName[0]);
                }

                aResult.AddRange(CheckedNames(aNode.Nodes));
            }
        }

        return aResult;
    }
Josef
  • 2,648
  • 5
  • 37
  • 73
  • Yes, but it depends. Ti si one of examples of my list. So the requirement is to list all checked items but exclude "root" and "subroots" if they have subitems. For better picture what I need is to get list of checked items, when it comes to code 556311200 it skips but print subitems 18566310, 103511210 and so on... – Josef Nov 26 '16 at 11:52
  • Just pay attention to the TreeNode.Level property. – Hans Passant Nov 26 '16 at 14:35
  • @HansPassant It seems the OP needs to exclude some nodes which has child nodes. For example highlighted nodes in image. `TreeNode.Level` will not help to distinguish between those root items. Some of them has children and some of them doesn't have. – Reza Aghaei Nov 26 '16 at 15:24

2 Answers2

1

You can use Descendants extension methods to get descendants for a TreeView or a TreeNode. Then after getting nodes, you can use a where clause to filter those nodes which has child nodes:

var result = treeView1.Descendants().Where(x=>x.Checked && x.Nodes.Count==0).ToList();

I've created a few useful extension methods for getting descendants and ancestors in tree view. To see them, take a look at these posts:

You can learn more about Extension Methods.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Those extension methods seem complicated at first look, but are really useful and reusable. Then you can wrote some complicated task like this, readable, clean and in a single line of code :) – Reza Aghaei Nov 29 '16 at 06:13
1

A simple recursion should do the trick like this (just a sketch, not tested):

void Main()
{
   var startingNode = <root/first node of the treeview>;
   var aResult = new List<String>();

   CheckedNames(startingNode, aResult);
}

void CheckedNames(TreeNode node, List<String> result)
{
   do
   {
      if (node.FirstNode != null)
      {
         CheckedNames(node.FirstNode, result);
      }
      else
      {
         if (node.Checked)
         {
            // use your regex here
            result.Add(node.Text);
         }
      }

      node = node.NextNode;
   } while (node != null);
}

Hope it helps.

Jürgen Röhr
  • 886
  • 6
  • 10