比如我的listview中有两个记录一个是“李伟”另一个是“李伟三”但是通过下面语言查找时只能查找出第一个和条件匹配的后面的就无法查询,我觉得是差了一个循环条件,但不知怎么写,请各位高手帮帮忙。(就是想像word那样查找到一条记录后还可以继续查找下一个符合条件的记录)
Private Sub cmdFind_Click()
    Dim i As Integer
    
    If Trim(txtFindCorp.Text) = "" Then Exit Sub    For i = 1 To lvwCorpPerson.ListItems.Count
        If InStr(1, lvwCorpPerson.ListItems(i).SubItems(2), Trim(txtFindCorp.Text), vbTextCompare) > 0 Then
            lvwCorpPerson.ListItems(i).EnsureVisible
            lvwCorpPerson.ListItems(i).Selected = True
            lvwCorpPerson.SetFocus
            Exit Sub
        End If
    Next i
    
End Sub

解决方案 »

  1.   

     If   InStr(1,   lvwCorpPerson.ListItems(i).SubItems(2),   Trim(txtFindCorp.Text),   vbTextCompare)   >   0   Then 
                            lvwCorpPerson.ListItems(i).EnsureVisible 
                            lvwCorpPerson.ListItems(i).Selected   =   True 
                            lvwCorpPerson.SetFocus 
                            Exit   Sub '这个去掉
    End   If 
      

  2.   

    各位老大,我去掉的就是这个exit sub,但是还是不行。
      

  3.   

    是这样的如果保留exit   sub则查到第一个符合条件的就结束了再点查询得到的还是那个结果,如果去掉exit   sub则焦点会直接锁定到最后一个符合条件的结果上,这样的话不能实现点一次查询得到一个结果再点一次则得到第二条结果的目的。
      

  4.   

    Private Sub cmdFind_Click()
        Dim i As Integer    If Trim(txtFindCorp.Text) = "" Then Exit Sub
        
        If lvwCorpPerson.SelectedItem Is Nothing Then
            i = 1
        Else
            i = lvwCorpPerson.SelectedItem.Index + 1
        End If
        For i = i To lvwCorpPerson.ListItems.Count
            If InStr(1, lvwCorpPerson.ListItems(i).SubItems(2), Trim(txtFindCorp.Text), vbTextCompare) > 0 Then
                lvwCorpPerson.ListItems(i).EnsureVisible
                lvwCorpPerson.ListItems(i).Selected = True
                lvwCorpPerson.SetFocus
                Exit Sub
            End If
        Next i
    End Sub
      

  5.   

    '需要循环查找如下修改
    Private Sub cmdFind_Click()
        Dim i As Long, lLast As Long, lCount As Long    If Trim(txtFindCorp.Text) = "" Then Exit Sub
        lCount = lvwCorpPerson.ListItems.Count
        
        If lvwCorpPerson.SelectedItem Is Nothing Then
            lLast = 0
        Else
            lLast = lvwCorpPerson.SelectedItem.Index
        End If
        For i = 1 To lCount
            With lvwCorpPerson.ListItems(((lLast + i) Mod lCount) + 1)
                If InStr(1, .SubItems(2), Trim(txtFindCorp.Text), vbTextCompare) > 0 Then
                    .EnsureVisible
                    .Selected = True
                    lvwCorpPerson.SetFocus
                    Exit Sub
                End If
            End With
        Next i
        MsgBox "无匹配项。", vbInformation
    End Sub
      

  6.   

    首先你要设置可以多选,然后
    Private   Sub   cmdFind_Click()
            Dim   i   As   Integer
           
            If   Trim(txtFindCorp.Text)   =   " "   Then   Exit   Sub        For   i   =   1   To   lvwCorpPerson.ListItems.Count
                    If   InStr(1,   lvwCorpPerson.ListItems(i).SubItems(2),   Trim(txtFindCorp.Text),   vbTextCompare)   >   0   Then
                            lvwCorpPerson.ListItems(i).EnsureVisible
                            lvwCorpPerson.ListItems(i).Selected   =   True
                            
                           
                    End   If
            Next   i
           
    End   Sub
    lvwCorpPerson.SetFocus '放在这里试试看