VB and VBA Users Source Code: Searching the contents of a listview
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Searching the contents of a listview
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Friday, September 14, 2001
Hits:
1302
Category:
Windows Forms/GUI/Controls/Graphics
Article:
The function below searches a listview for a specific item. The code will search the text, subitems and tags of the listview items. Public Enum elvSearch elvSearchText = 1 elvSearchSub = 2 elvSearchTag = 4 End Enum 'Purpose : Finds and selects and item in a listview 'Inputs : sFileName The path and file name of the component to register. ' lvFind The listview to search for the item in. ' [eValueType] The type of values to search: ' 1 = Searches the text items. ' 2 = Searches sub items. ' 4 = Searches the item tags. ' [lSearchFor] The type of matching required: ' lvwWhole = Find whole word. ' lvwPartial = Find a partial match. ' [lIndexBeginFrom] The item index to begin the search from, for recursive ' searches. See the index property of the listitem. ' property of the listitem. 'Outputs : N/A 'Author : Andrew Baker 'Date : 25/11/2000 03:17 'Notes : 'Revisions : Function ListViewFindItem(sFindItem As String, lvFind As ListView, Optional eValueType As elvSearch = elvSearchText + elvSearchSub + elvSearchTag, Optional lSearchFor As Long = lvwPartial, Optional lIndexBeginFrom As Long = 1) As ListItem On Error Resume Next 'Try to find item If eValueType And elvSearchText Then 'Search text Set ListViewFindItem = lvFind.FindItem(sFindItem, lvwText, lIndexBeginFrom, lSearchFor) End If If eValueType And elvSearchSub And (ListViewFindItem Is Nothing) Then 'Search subitems Set ListViewFindItem = lvFind.FindItem(sFindItem, lvwText, lIndexBeginFrom, lSearchFor) End If If eValueType And elvSearchTag And (ListViewFindItem Is Nothing) Then 'Search tags Set ListViewFindItem = lvFind.FindItem(sFindItem, lvwText, lIndexBeginFrom, lSearchFor) End If If (ListViewFindItem Is Nothing) = False Then 'Found a matching item, display it. Set lvFind.SelectedItem = ListViewFindItem lvFind.SelectedItem.EnsureVisible End If On Error Goto 0 End Function 'Demonstration routine Sub Test() Dim oListItem As ListItem 'Search for "My Search Text" Set oListItem = ListViewFindItem("My Search Text", lvAccounts, elvSearchText) If (oListItem Is Nothing) = False Then 'Found text in listview, now select item Set lvAccounts.SelectedItem = oListItem.Select End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder