hi,
how can i get the index a an item in listview? what i want is to search for a text in one of the columns in the listview, then get the index of the first listviewitem that contains the text
thanks...
Printable View
hi,
how can i get the index a an item in listview? what i want is to search for a text in one of the columns in the listview, then get the index of the first listviewitem that contains the text
thanks...
VB Code:
Public Sub SearchListViewColumn(ByRef lvwView As ListView, ByVal sSearchText As String, _ ByVal Column As Byte, Optional ByVal bMatchCase As Boolean = False) '============================================================================== ' Purpose : Search a ListView Column, highlighting all matches ' Inputs : ListView, sSearchText, lColumn ' Returns : HighLights all found Items in ListView Column ' Calls : SearchListViewColumn(ListView, sSearchText, Column) ' mod_ListView ' Use in : Any Procedure '============================================================================== On Error GoTo Error_ErrHandler Dim x As Long = 0 Dim Columns As Byte = lvwView.Columns.Count - 1 Dim Item As ListViewItem If Len(sSearchText) = 0 Then Exit Sub With lvwView .BeginUpdate() .FullRowSelect = True .MultiSelect = True For Each Item In .SelectedItems Item.Selected = False Next For x = 0 To .Items.Count - 1 If bMatchCase = True Then ' Search Item using MatchCase If Column = 0 Then If .Items(x).Text Like sSearchText _ And Len(.Items(x).Text) = Len(sSearchText) Then .Items(x).Selected = True .Items(x).EnsureVisible() End If ElseIf Column > 0 Then For Column = 0 To Columns ' Search SubItems using MatchCase If .Items(x).SubItems(Column).Text Like sSearchText _ And Len(.Items(x).SubItems(Column).Text) = Len(sSearchText) Then .Items(x).Selected = True .Items(x).EnsureVisible() End If Next End If ElseIf bMatchCase = False Then ' Search without MatchCase If Column = 0 Then If .Items(x).Text.ToLower Like sSearchText.ToLower _ And Len(.Items(x).Text) = Len(sSearchText) Then ' Search Item .Items(x).Selected = True .Items(x).EnsureVisible() End If ElseIf Column > 0 Then For Column = 1 To Columns ' Search SubItems If .Items(x).SubItems(Column).Text.ToLower Like sSearchText.ToLower _ And Len(.Items(x).SubItems(Column).Text) = Len(sSearchText) Then .Items(x).Selected = True .Items(x).EnsureVisible() End If Next End If End If Next .Focus() .EndUpdate() End With Exit Sub Error_ErrHandler: End Sub
All matches are selected so you can easyly enumerate the index of each match.
greetz
Cheffe0815