I'm trying to search a TreeView for a particular string, once I have found the index of the node, I wish to return its index and change the back color of that particular node, however my current code does not seem to be returning any matches, unless it's the root node:
private void ApplyRulesetColors()
{
foreach (var rule in dictOverwriteEntries)
{
int iResultIndex = SearchTreeView(rule.Key, tvDirectoryStructure.Nodes);
if (iResultIndex > -1)
{
switch (rule.Value)
{
case Operations.Overwrite:
tvDirectoryStructure.Nodes[iResultIndex].BackColor = Color.Red;
break;
case Operations.Delete:
break;
case Operations.None:
break;
default:
break;
}
}
}
}
This is the function that should be searching the treeview:
private int SearchTreeView(string p_sSearchTerm, TreeNodeCollection p_Nodes)
{
foreach (TreeNode node in p_Nodes)
{
if (node.Text == p_sSearchTerm)
{
return node.Index;
}
if (node.Nodes.Count > 0)
SearchTreeView(p_sSearchTerm, node.Nodes);
}
return -1;
}
At the moment that will only ever match on the root node value and return 0, even though (as far as I can tell) it should be searching through the entire tree including child nodes.
Thank you.