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?
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.
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With