本文介绍了在listBox中搜索指定的字符串VB6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为lstSerial的列表框和一个名为txtSerials的文本框.我要做的是搜索lstSerial,以查找在txtSerials中输入的字符串.我在Microsoft Visual Basic 6.0中使用VB6,并且在查找文档方面花费了很多时间.

I have a listbox called lstSerial and a textbox called txtSerials. What I want to do is search lstSerial for the string that's entered in txtSerials. I'm using VB6 in Microsoft Visual Basic 6.0, and I'm having a terrible time finding documentation.

谢谢.

推荐答案

@AlexK的答案在技术上是正确的-是的-它可以工作,但这不是首选的方式.为此,有一个API调用:

@AlexK's answer is technically correct - yes - it will work, but it's not the preferred way to go. There is an API call for this very purpose:

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

'constants for searching the ListBox
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Const LB_FINDSTRING = &H18F

'function to get find an item in the Listbox
Public Function GetListBoxIndex(hWnd As Long, SearchKey As String, Optional FindExactMatch As Boolean = True) As Long

    If FindExactMatch Then
        GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRINGEXACT, -1, ByVal SearchKey)
    Else
        GetListBoxIndex = SendMessage(hWnd, LB_FINDSTRING, -1, ByVal SearchKey)
    End If

End Function

所以您要这样做:

lstSerial.ListIndex = GetListBoxIndex(lstSerial.hWnd, txtSerials.Text)

来源

这篇关于在listBox中搜索指定的字符串VB6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:58