Results 1 to 2 of 2

Thread: Search in treeview

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Search in treeview

    I am trying to create a "filter" type treeview search where, when you start typing in a textbox, it will go to that treenode.

    so if I had:

    f
    fi
    fish
    fishe
    fisher


    and start to type "f", it would go to the first instance found of nodes beginning with "f", and if I typed in the next letter, it would highlight/go to that instance...and so on

    I know there is a Find method in the TreeView Nodes collection but that is not quite working for me as you need to give it the full name of the treeview node name.

    Any ideas?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Search in treeview

    Here's a refinement of the code I posted in your other thread:
    CSharp Code:
    1. private TreeNode FindNode(TreeNodeCollection nodes, string text)
    2. {
    3.     TreeNode match = null;
    4.  
    5.     foreach (TreeNode node in nodes)
    6.     {
    7.         if (node.Text.StartsWith(text))
    8.         {
    9.             // The current node is the first match.
    10.             match = node;
    11.             break;
    12.         }
    13.         else if ((match = this.FindNode(node.Nodes, text)) != null)
    14.         {
    15.             // A descendant of the current node is the first match.
    16.             break;
    17.         }
    18.     }
    19.  
    20.     return match;
    21. }
    Just note that, while I wrote that code in an IDE I never actually tested it, so I can't guarantee it 100%. I'll leave the testing to you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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