VB and VBA Users Source Code: Search a treeview for a item
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Search a treeview for a item
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, February 26, 2001
Hits:
941
Category:
Windows Forms/GUI/Controls/Graphics
Article:
The following code can be used to search a treeview for an item of text: 'Purpose : Performs a recallable search on a treeview. 'Inputs : tvFind The treeview to search. ' sFindItem The text to search for (supports wildcards) ' [bSearchAll] If True will search all nodes of the treeview, else ' will only search top level nodes. ' [lItemIndex] If specified will return this matching index. ' eg, if set to 2 then will return the second node which matches the search criteria. 'Outputs : Returns the a matching treeview node or nothing if the item is not found 'Author : Andrew Baker 'Date : 26/02/2001 21:21 'Notes : 'Assumptions : Function TreeViewFindNode(tvFind As TreeView, ByVal sFindItem As String, Optional bSearchAll As Boolean = True, Optional lItemIndex As Long = 1) As Node Dim oThisNode As Node, bSearch As Boolean, lInstance As Long sFindItem = UCase$(sFindItem) bSearch = True For Each oThisNode In tvFind.Nodes If bSearchAll = False Then 'Only Search Top Level Nodes If (oThisNode.Parent Is Nothing) = False Then bSearch = False Else bSearch = True End If End If If bSearch Then If (UCase$(oThisNode.Text) Like sFindItem) = True Then lInstance = lInstance + 1 If lInstance >= lItemIndex Then 'Found matching item Set TreeViewFindNode = oThisNode Exit For End If End If End If Next End Function 'Demonstration routine, using repeated calls to find every matching instance of "Andrew Baker" 'in a treeview Sub Test() Dim lItemIndex As Long, oFoundNode As Node Do lItemIndex = lItemIndex + 1 Set oFoundNode = TreeViewFindNode(tvAccounts, "Andrew Baker", True, lItemIndex) If tvAccounts Is Nothing Then 'Didn't find any more items Exit Do End If oFoundNode.EnsureVisible If MsgBox("Found " & oFoundNode.Text & vbNewLine & "Find next matching item? ", vbQuestion + vbYesNo) = vbNo Then Exit Do End If Loop End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder