Visual Basic > Forms
Search a treeview for a item
Search a treeview for a item 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 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 Sub Test() Dim lItemIndex As Long, oFoundNode As Node Do lItemIndex = lItemIndex + 1 Set oFoundNode = TreeViewFindNode(tvAccounts, "Red", 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
Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows