-4

I have treeView1, I need to create treeview2 using nodes of treeView1 when treeNode.Text == myString. So I have to traverse all nodes of treeView1, and if treeNode.Text == myString, then I should add this node to treeView2, and if any of this node's childs has this property (treeNode.Text == myString), then I should also add this node to treeView2, even if .Text != myString. In other words, treeView2 object is a filtered treeView1 object.

Could someone give me an idea how to implement this ?

Romz
  • 1,437
  • 7
  • 36
  • 63
  • 1
    What is wrong with this answer [here](http://stackoverflow.com/a/12388467/932418) – L.B Sep 13 '12 at 20:16
  • 1
    possible duplicate of [Is there a method for searching for TreeNode.Text field in TreeView.Nodes collection?](http://stackoverflow.com/questions/12388249/is-there-a-method-for-searching-for-treenode-text-field-in-treeview-nodes-collec) – L.B Sep 13 '12 at 20:17
  • @L.B., it doesn't create a new treeView from old one – Romz Sep 13 '12 at 20:20
  • So you don't want to spend a few minutes on it. – L.B Sep 13 '12 at 20:21
  • 3
    -1 Sounds like you have an idea of how to implement this. Are you looking for someone to write the code for you? http://www.WhatHaveYouTried.com – JDB Sep 13 '12 at 20:22
  • In this case I need to use stack, because I don't know should I add the node or not(it depends on weather this node's child has `.Text == myString` – Romz Sep 13 '12 at 20:23
  • Usually, an algorithm which can be implemented with stack can also be implemented recursively. See the link I posted again. – L.B Sep 13 '12 at 20:25
  • @Cyborgx37, I can find and add all nodes with this property, I can't build a treeview with all this nodes – Romz Sep 13 '12 at 20:27
  • also I can't find and add nodes that doesn't have this property, but its child does – Romz Sep 13 '12 at 20:29
  • @L.B, I've spend all day on it – Romz Sep 13 '12 at 20:34
  • 1
    @William just to be clear, do you want to learn how to do it, or expect someone to write the code for you? – L.B Sep 13 '12 at 20:36
  • @Cyborgx37, great article, I'm not developer yet though – Romz Sep 13 '12 at 20:37
  • @L.B., I'll be glad if someone explain me how to do it, just not sure you get what I want(because you post link to my previous question) – Romz Sep 13 '12 at 20:40
  • @William No I posted a link to my answer for your prev. question. Just debug it and try to understand what it does. I am always here for further questions – L.B Sep 13 '12 at 20:45

1 Answers1

0

you should use the recursive approach with TreeViews, so try this its working fine :

 ...

        foreach (TreeNode item in treeView1.Nodes)//in case you have multiple rootnodes
            treeView2.Nodes.Add((TreeNode)item.Clone());//clone all treeview1 nodes into treeview2

        foreach (TreeNode node in treeView2.Nodes)//in case you have multiple rootnodes
            AmISelected(node, "momo");//filter all nodes recursively

 ...

with

private bool AmISelected(TreeNode root, string myString)
    {
        bool bfound = false;
        if (root.Text == myString)
            bfound = true;// I do have myString

        for (int i = root.Nodes.Count - 1; i >= 0; i--)
            if (AmISelected(root.Nodes[i], myString))
                bfound = true;// I've a child who have myString

        if (!bfound) //if i don't have myString neither any of my children
            if (root.Parent == null) // is it a root node
                treeView2.Nodes.Remove(root);
            else
                root.Parent.Nodes.Remove(root);

        return bfound;

    }

enter image description here

S3ddi9
  • 2,121
  • 2
  • 20
  • 34
  • Why did you use `foreach (TreeNode node in treeView2.Nodes)` ?, we need to write only rootnode as first argument, like this: `AmISelected(treeView2.Nodes[0], "momo");`, and it works fine – Romz Sep 14 '12 at 09:38
  • also, I don't know why, but `foreach (TreeNode node in treeView.Nodes)` runs only one time, like treenodecollection contains only rootnode, so I can't make a copy of the tree. I filtered `treeView1` itself. – Romz Sep 14 '12 at 09:45
  • you didn't specify that your treeview has one node, so i took the general case !, if your treeview contains only one root node so `treeView2.Nodes[0]` – S3ddi9 Sep 14 '12 at 12:51
  • and for the 2nd comment, the first loop clone the root nodes **with theirs children**, *may I ask if you have tried this code, i've tried it and its working perfectly with multi root nodes.* – S3ddi9 Sep 14 '12 at 12:55