在列表框中搜索指定的字符串 VB6 [英] Searching a listBox for a specified string VB6

查看:13
本文介绍了在列表框中搜索指定的字符串 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)

来源

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆