Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find TreeView Node by Value

All my TreeView nodes have a unique ID for their Node Depth.

I want to set Checked=True on the TreeView Node which matches a certain value.

Currently I'm doing the following:

Dim value As Integer = 57

For Each n As TreeNode In tvForces.Nodes
   If n.Value = value Then n.Checked = True
Next

Is there a better way of finding the Node which I want to set as Checked=True rather than looping through each node?

I'm looking for something like:

Dim value As Integer = 57

n.FindNodesByValue(value)(0).Checked = True

Is there anything like this that I can use?

like image 936
Curtis Avatar asked Sep 15 '11 11:09

Curtis


2 Answers

Pseudocode (c#) to demonstrate an idea using LINQ Where() + List.ForEach():

nodes.Where(node => node.Value == "5")
     .ToList()
     .ForEach((node => node.Checked = true));

See MSDN following the links above for VB.NET syntax of both methods.

like image 63
sll Avatar answered Oct 14 '22 22:10

sll


                foreach (TreeNode node in TreeView1.Nodes)
                {
                    if (node.Value == "8")
                    {
                        node.Checked = true;
                    }
                    foreach (TreeNode item1 in node.ChildNodes)
                    {
                        if (item1.Value == "8")
                        {
                            item1.Checked = true;
                        }
                    }
                }               
like image 26
Dhiraj Mane Avatar answered Oct 14 '22 21:10

Dhiraj Mane