Visual Basic Programming Code Examples Visual Basic > Forms Code Examples Searching the contents of a listview Searching the contents of a listview 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 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