Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching treeview

Status
Not open for further replies.

WiseNewbie

Programmer
Jan 9, 2004
45
AU
Hello,

I was wondering whether there is there a quicker way to search a treeview control rather than looping through all the nodes... The reason I ask is we've created a similar feature to that of the HTML Help's Index tab - so you enter some text and it will search for the item in a Treeview(we chose this and not a Listview because we can indent the items and not a lIstbox because it was a little better looking) so anyhow as the user types we need to find the closest match and at the moment its looping through all the nodes to search for it.

Is there a faster method via API to achieve this?
 
Don't know of an API call that will do it, especially since you are only doing a partial search, rather than an exact match.

As a cludge, you might consider having a collection which matches the treeview contents one-to-one.
The collection would have description and absolute index for every item in the treeview, pre-sorted in alphabetical order.
You would need to create a class or type to hold this:

..<type or class>
desc as string
idx as long
<end type or class>

Once you have this collection, every time someone adds a letter to the textbox, you then need to search the collection for the nearest match.
Searching a sorted list is very quick using a binary chop search, but I imagine that even simply looping through the collection might be faster than looping through the treeview, because you simply stop when you have something suitable, instead of trying out every other item in case it is a better 'fit'.
Give it a try.
 
Sadly, the TreeView control doesn't have an API call for directly searching the nodes for text. In theory you {b]have{/b] to iterate through each node looking for the match.

Of course, if you knew the unique key for each Node item, then you could imediately retrieve the rleavnet Node object. So, why no use a Trieeviw and a Listview. For each Node item you add to the Treeview also add a ListItem with the same .Text property and the same .Key. You can now do a FindItem (using lvwPartial as the match method)against the ListView, retrieve the found item's key, and then retrieve the correct Node item from the Treeview by using that Key
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top