CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Dec 2009
    Posts
    18

    [RESOLVED] How to loop a treeview winforms

    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;
                            }
                        }
                    }
                }
            }
    Last edited by Omegadarkest; March 7th, 2012 at 12:42 AM. Reason: Figured out issue

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured