在列表框中搜索指定字符串 VB6

发布于 2025-01-05 03:14:35 字数 154 浏览 0 评论 0原文

我有一个名为 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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别把无礼当个性 2025-01-12 03:14:35

@AlexK 的答案在技术上是正确的 - 是的 - 它会起作用,但这不是首选的方法。为此目的有一个 API 调用:

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)

来源

@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

So you want to do this:

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

Source

入怼 2025-01-12 03:14:35

文件; http://msdn.microsoft.com/en-我们/库/aa267225(v=VS.60).aspx

dim find as string,i as long,found as boolean
find=txtSerials.text

for i=0 to lstserial.listcount - 1
    if strcomp(find, lstSerial.list(i), vbTextcompare)=0 then
        found = true
        lstSerial.setfocus
        lstSerial.listindex= i
        exit for
    end if
next

if not found then msgbox "not found ..."

Docs; http://msdn.microsoft.com/en-us/library/aa267225(v=VS.60).aspx

dim find as string,i as long,found as boolean
find=txtSerials.text

for i=0 to lstserial.listcount - 1
    if strcomp(find, lstSerial.list(i), vbTextcompare)=0 then
        found = true
        lstSerial.setfocus
        lstSerial.listindex= i
        exit for
    end if
next

if not found then msgbox "not found ..."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文