注册 登录
编程论坛 VB6论坛

再问查找的问题

chen3bing 发布于 2020-02-25 15:15, 1159 次点击
刚问了查找的问题,版主解答了。
后来测试了一下,觉得不太好,改了一下。
代码如下:
Dim curr As Integer

Dim i As Integer
Dim a As String
a = Dialog.Text1
curr = ConfigureTestType.List2.ListIndex

  For i = curr + 1 To ConfigureTestType.List2.ListCount - 1
        If InStr(1, ConfigureTestType.List2.List(i), a) > 0 Then
            ConfigureTestType.List2.ListIndex = i
            Exit Sub
        End If
  Next
  For i = 0 To curr
        If InStr(1, ConfigureTestType.List2.List(i), a) > 0 Then
            ConfigureTestType.List2.ListIndex = i
            Exit Sub
        End If
  Next
  MsgBox "没找到!"
没找到,提示"没找到",这个有个问题,如果找到,从头找到尾,之后又从头开始找。
我想实现的效果是:
点击查找,直接从头找,第一条显示为蓝色。没找到提示“没找到”
如果找到,显示为蓝色,逐条显示为蓝色。找到最后,提示到文件末尾,或者干脆找到最后一条,就不返回去找了。
谢谢!
3 回复
#2
chen3bing2020-02-25 16:10
wmf2014大侠请多指点,谢谢!
#3
chen3bing2020-02-26 10:11
wfm2014大神,看到了吗?
#4
wmf20142020-02-26 18:48
程序代码:
Sub findlist(a As String)
  Dim i As Integer
  For i = List1.ListIndex + 1 To List1.ListCount - 1
    If InStr(List1.List(i), a) > 0 Then Exit For
  Next
  If i = List1.ListCount Then
    MsgBox "没找到包含“" & a & "”的项目"
    List1.ListIndex = -1
  Else
    List1.ListIndex = i
  End If
End Sub

Private Sub Command1_Click()
  '找包含"版主"字符的项
  findlist "版主"
End Sub

Private Sub Form_Load()
  List1.AddItem "刚问了查找的问题"
  List1.AddItem "版主解答了"
  List1.AddItem "后来测试了一下"
  List1.AddItem "觉得不太好"
  List1.AddItem "改了一下"
  List1.AddItem "代码如下"
  List1.AddItem "版主解答了"
End Sub
1