Results 1 to 2 of 2

Thread: viewlist,index Question!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227

    viewlist,index Question!

    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...

  2. #2
    New Member
    Join Date
    Aug 2004
    Location
    Germany
    Posts
    11
    VB Code:
    1. Public Sub SearchListViewColumn(ByRef lvwView As ListView, ByVal sSearchText As String, _
    2.     ByVal Column As Byte, Optional ByVal bMatchCase As Boolean = False)
    3.     '==============================================================================
    4.     ' Purpose      :  Search a ListView Column, highlighting all matches
    5.     ' Inputs       :  ListView, sSearchText, lColumn
    6.     ' Returns      :  HighLights all found Items in ListView Column
    7.     ' Calls        :  SearchListViewColumn(ListView, sSearchText, Column) ' mod_ListView
    8.     ' Use in       :  Any Procedure
    9.     '==============================================================================
    10.     On Error GoTo Error_ErrHandler
    11.  
    12.     Dim x As Long = 0
    13.     Dim Columns As Byte = lvwView.Columns.Count - 1
    14.     Dim Item As ListViewItem
    15.  
    16.     If Len(sSearchText) = 0 Then Exit Sub
    17.  
    18.     With lvwView
    19.         .BeginUpdate()
    20.         .FullRowSelect = True
    21.         .MultiSelect = True
    22.  
    23.         For Each Item In .SelectedItems
    24.             Item.Selected = False
    25.         Next
    26.  
    27.         For x = 0 To .Items.Count - 1
    28.             If bMatchCase = True Then ' Search Item using MatchCase
    29.                 If Column = 0 Then
    30.                     If .Items(x).Text Like sSearchText _
    31.                         And Len(.Items(x).Text) = Len(sSearchText) Then
    32.                         .Items(x).Selected = True
    33.                         .Items(x).EnsureVisible()
    34.                     End If
    35.                 ElseIf Column > 0 Then
    36.                         For Column = 0 To Columns ' Search SubItems using MatchCase
    37.                             If .Items(x).SubItems(Column).Text Like sSearchText _
    38.                                 And Len(.Items(x).SubItems(Column).Text) = Len(sSearchText) Then
    39.                                 .Items(x).Selected = True
    40.                                 .Items(x).EnsureVisible()
    41.                             End If
    42.                         Next
    43.                 End If
    44.  
    45.             ElseIf bMatchCase = False Then ' Search without MatchCase
    46.                 If Column = 0 Then
    47.                     If .Items(x).Text.ToLower Like sSearchText.ToLower _
    48.                         And Len(.Items(x).Text) = Len(sSearchText) Then ' Search Item
    49.                         .Items(x).Selected = True
    50.                         .Items(x).EnsureVisible()
    51.                     End If
    52.                 ElseIf Column > 0 Then
    53.                     For Column = 1 To Columns ' Search SubItems
    54.                         If .Items(x).SubItems(Column).Text.ToLower Like sSearchText.ToLower _
    55.                             And Len(.Items(x).SubItems(Column).Text) = Len(sSearchText) Then
    56.                             .Items(x).Selected = True
    57.                             .Items(x).EnsureVisible()
    58.                         End If
    59.                     Next
    60.                 End If
    61.             End If
    62.         Next
    63.  
    64.         .Focus()
    65.         .EndUpdate()
    66.     End With
    67.  
    68. Exit Sub
    69. Error_ErrHandler:
    70.  
    71. End Sub

    All matches are selected so you can easyly enumerate the index of each match.

    greetz

    Cheffe0815

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width