Link to home
Create AccountLog in
Avatar of Ragine
Ragine

asked on

Remove treeview nodes...

Hello Experts,

- I have a treeview with around 1k nodes. I want to filter them by name, so if a nodes name is "hello" i want to filter it and have it removed.

- Also is there a way to find a node by name and than select it? Count all nodes?

I did a bit of goggling and didn't find what I was exactly looking for.


Thank you for you time.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

A Tree has a collection of nodes - TreeNodeCollection.  That has a Find member which you can use to search for a node (optionally including children in the search)

Something like
TreeNode[] foundNodes = mytree.Nodes.Find("foo");
Avatar of Ragine
Ragine

ASKER

Alright thanks for the assistance. But the next step will be removing the node. So after finding the node how can I remove it? Using >>AndyAinscow<< method?

TreeNode[] nodefound = tree.Nodes.Find("node", false);

Open in new window

Does this do the job:

foreach(TreeNode tn in nodeFound)
{
  tree.Nodes.Remove(tn);
}
Avatar of Ragine

ASKER

No, doesn't seem to work.
Oops, sorry - didn't look too closely.  Contains doesn't search on the name so it doesn't do what you want.
I don't know if this is suitable.  if the name of the node is the same as the text then the code does work:

User generated image
button code
        private void button1_Click(object sender, EventArgs e)
        {
            TreeNode[] nodeFound = this.treeView1.Nodes.Find("One", true);
            foreach (TreeNode tn in nodeFound)
            {
                this.treeView1.Nodes.Remove(tn);
            }
            this.Refresh();
        }

Open in new window


User generated image
Avatar of Ragine

ASKER

AndyAinscow,

- Thank you for your help. This actually didn't really do anything, but I've tested it with a test tree view and it worked. So i believe it has to do something with the code that's actually displaying/adding the nodes.

private void LoadTree(SystemAccessibleObject sao)
        {

            TreeNode curr = new TreeNode(sao.ToString());
            SystemAccessibleObject[] children = sao.Children;
            foreach (SystemAccessibleObject o in children)
            {
                TreeNode tn = new TreeNode(o.ToString());
                tn.Tag = o;
                LoadTreeChildren(tn);
            }
           
        }

        private void LoadTreeChildren(TreeNode curr)
        {
            
            SystemAccessibleObject sao = (SystemAccessibleObject)curr.Tag;
            SystemAccessibleObject[] children = sao.Children;
            foreach (SystemAccessibleObject o in children)
            {
                TreeNode tn = new TreeNode(o.ToString());
                if (o.ToString() != "[grouping]")
                {
                    tn.Tag = o;

                    //tree.Nodes.Add(tn);//
                    //MessageBox.Show(tn.Text);
                    tree.Nodes.Add(tn);//
                }
            }
        }

Open in new window


This is what displays the nodes, I've edited the code to my needs poorly so It's bad. Also for some reason the "if (o.ToString() != "[grouping]")" doesn't work?

I greatly appreciate your help. Thank you.
I think when you add the nodes you need to explicitly set the text AND the name to be the same.
Avatar of Ragine

ASKER

I don't quit understand?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Ragine

ASKER

Alright I've altered the code and it seems to work, but only for nodes that come up once. But for some reason when more than one comes up it doesn't work for that node.


- scratch that... it works on a test treeview but not for the original.

- scratch that again it worked, for some reason the nodes have been added with a "space" infront than the text.

Thank you for all your help. I greatly appreciate it.
Avatar of Ragine

ASKER

Amazing help!!
Glad it works.  Just remember it is using the name of the node to find them, not the text being displayed.