For the life of me I can't figure this out nor can I find it on google that will actually work.
WinForms TreeView Control
.net 4
I'm trying to search all treeview nodes including childs and select the current node.
The Search
Code:
private void txtTVSearch_TextChanged(object sender, EventArgs e)
{
if (txtTVSearch.Text == string.Empty)
{
tvAccounts.SelectedNode = tvAccounts.Nodes[0];
}
else
{
cbExpand.Checked = true;
foreach (TreeNode node in tvAccounts.Nodes)
{
if (node.Text.Substring(0, txtTVSearch.Text.Length).ToLower() == txtTVSearch.Text.ToLower())
{
tvAccounts.SelectedNode = node;
}
}
}
Here is how I am adding the child nodes
Code:
private void addChildNode(string parentNode, string childNodeName)
{
tvAccounts.Nodes[parentNode].Nodes.Add(childNodeName);
}
HERE IS MY SOLUTION THAT I GOT WORKING
Code:
private void txtTVSearch_TextChanged(object sender, EventArgs e)
{
if (txtTVSearch.Text == string.Empty)
{
tvAccounts.SelectedNode = tvAccounts.Nodes[0];
}
else
{
cbExpand.Checked = true;
for (int i = 0; i < tvAccounts.Nodes.Count; i++)
{
for (int c = 0; c < tvAccounts.Nodes[i].Nodes.Count; c++)
{
if (tvAccounts.Nodes[i].Nodes[c].Text.Substring(0, txtTVSearch.Text.Length).ToLower() == (txtTVSearch.Text.ToLower()))
{
tvAccounts.SelectedNode = tvAccounts.Nodes[i].Nodes[c];
break;
}
}
}
}
}