Carl Harrison
Carl Harrison

Reputation: 155

How to find a Treenode and set Checked=True

I am trying to use the Find method to find a node in a TreeView and then set the CheckBox of that node to True.

Dim node As TreeNode() = TreeView1.Nodes.Find(FindStr, True)

The code finds the node fine, but when I try something like

TreeView1.Nodes(node).Checked=True

it's not having any of it, node isn't returning an Integer. I presume it is returning a collection of what it's found, which is fine. Since the keys are unique it is only going to return the one node, if it does find it. But I'm still not having any luck being able to check its box.

I've been googling for an answer for well over an hour, trying varying keywords just in case but I'm not getting any useful results back.

Upvotes: 1

Views: 2396

Answers (1)

MatSnow
MatSnow

Reputation: 7517

The Find-function returns an Array of nodes.

If the first found node should be checked, the following will work:

Dim nodes As TreeNode() = TreeView1.Nodes.Find(FindStr, True)

'Check if at least one node was found
If nodes.Length > 0 Then
    'Set Checked=True for the first found node (index 0)
    nodes(0).Checked = True
End If

Upvotes: 2

Related Questions