Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > VB6 > Поиск в ListBox и ListView


Автор: cardinal 31.5.2005, 16:07
ListBox:
Код

Declare Function SendMessage Lib "User32" Alias "SendMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const LB_FINDSTRINGEXACT = &H1A2

RetValue = SendMessage(lstList.hWnd, LB_FINDSTRINGEXACT, -1, ByVal FindString)

ListView:
Код

Public Function ListViewDoSearch(lstListName As ListView, strStringToFind As String, _
    Optional blnWholeWordOnly As Boolean, Optional blnCaseSensitive As Boolean) As Integer
Dim lngIndex As Long
Dim lngIndexSub As Long
Dim strCurrItem As String
If blnCaseSensitive = True Then strStringToFind = UCase(strStringToFind)
ListViewDoSearch = 0
If lstListName.ListItems.Count < 1 Then Exit Function
If lstListName.SelectedItem.Index = -1 Then lstListName.SelectedItem.Index = 1
For lngIndex = lstListName.SelectedItem.Index - -1 To lstListName.ListItems.Count
    If blnCaseSensitive = True Then
        strCurrItem = UCase(lstListName.ListItems.Item(lngIndex).Text)
    Else
        strCurrItem = lstListName.ListItems.Item(lngIndex).Text
    End If
    If blnWholeWordOnly = True Then
        If strCurrItem = strStringToFind Then GoTo Conclude
    Else
        If InStr(strCurrItem, strStringToFind) > 0 Then GoTo Conclude
    End If
    If lstListName.ColumnHeaders.Count > 1 Then
        For lngIndexSub = 1 To lstListName.ColumnHeaders.Count - 1
            If blnCaseSensitive = True Then
                strCurrItem = UCase(lstListName.ListItems.Item(lngIndex).SubItems(lngIndexSub))
            Else
                strCurrItem = lstListName.ListItems.Item(lngIndex).SubItems(lngIndexSub)
            End If
            If blnWholeWordOnly = True Then
                If strCurrItem = strStringToFind Then GoTo Conclude
            Else
                If InStr(strCurrItem, strStringToFind) > 0 Then GoTo Conclude
            End If
        Next lngIndexSub
    End If
Next lngIndex
Exit Function
Conclude:
    ListViewDoSearch = lngIndex
    lstListName.ListItems.Item(lngIndex).EnsureVisible
    lstListName.ListItems.Item(lngIndex).Selected = True
End Function

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)