Attribute VB_Name = "ListViewModule"
Option Explicit

Public Function IsInList(ByVal sText As String, lvList As ListView) As Boolean
    Dim iCnt As Integer
    Dim iListCount As Integer
    
    iListCount = lvList.ListItems.Count
    If iListCount <= 0 Then Exit Function
    
    For iCnt = 1 To iListCount
        If IsInListRow(iCnt, sText, lvList) Then
            IsInList = True
            Exit Function
        End If
    Next iCnt
End Function

Public Function IsInListRow(ByVal iRow As Integer, ByVal sText As String, lvList As ListView) As Boolean
    Dim iTmpIndex As Integer
    Dim iTmpCnt As Integer
    
    If IsInString(sText, lvList.ListItems.Item(iRow).Text) Then
        IsInListRow = True
        Exit Function
    End If
    
    iTmpIndex = lvList.ListItems.Item(iRow).ListSubItems.Count
    If iTmpIndex > 0 Then
        For iTmpCnt = 1 To iTmpIndex
            If IsInString(sText, lvList.ListItems.Item(iRow).SubItems(iTmpCnt)) Then
                IsInListRow = True
                Exit Function
            End If
        Next iTmpCnt
    End If
End Function

Public Function AttendInList(ByVal sText As String, ByVal iIndexStart As Integer, lvList As ListView) As Long
    Dim iCnt As Integer
    Dim bFound As Boolean
    
    AttendInList = 0
    
    sText = Trim(sText)
    If sText = "" Then Exit Function
    If lvList.ListItems.Count <= 0 Then Exit Function
    If iIndexStart < 1 Or iIndexStart > lvList.ListItems.Count Then Exit Function
    
    If iIndexStart + 1 <= lvList.ListItems.Count Then
        For iCnt = iIndexStart + 1 To lvList.ListItems.Count
            If IsInListRow(iCnt, sText, lvList) Then
                lvList.ListItems.Item(iCnt).Selected = True
                lvList.ListItems.Item(iCnt).EnsureVisible
                bFound = True
                AttendInList = iCnt
                Exit Function
            End If
        Next iCnt
    End If
    If bFound = False Then
        If iIndexStart - 1 > 0 Then
            For iCnt = 1 To iIndexStart - 1
                If IsInListRow(iCnt, sText, lvList) Then
                    lvList.ListItems.Item(iCnt).Selected = True
                    lvList.ListItems.Item(iCnt).EnsureVisible
                    bFound = True
                    AttendInList = iCnt
                    Exit Function
                End If
            Next iCnt
        End If
    End If
End Function
