Search Tools Links Login

Find faster a String in Combo or ListBox while typing (Using SendMessage API)


Visual Basic 6, or VB Classic

This code is useful to look at a string inside a ComboBox or ListBox, while you typing it

Original Author: Omar Vivas

Inputs

The String to find.

Assumptions

You must set the API Declaration in a Module

Returns

If the string is found, it is set position respective.

API Declarations

#If Win32 Then
Declare Function SendMessage Lib "User32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
#Else
Declare Function SendMessage Lib "User" _
(ByVal hWnd As Integer, ByVal wMsg As Integer, _
ByVal wParam As Integer, lParam As Any) As Long
#End If
Const CB_FINDSTRINGEXACT = &H158 'Search string Exact in ComboBox
Const LB_FINDSTRINGEXACT = &H1A2 'Search string Exact in ListBox
Const CB_FINDSTRING = &H14C'Search string to begin in ComboBox
Const LB_FINDSTRING = &H18F'Search string to begin in ListBox

Code

Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim CB As Long
Dim FindString As String
Const CB_ERR = (-1)
Const CB_FINDSTRING = &H14C
If KeyAscii < 32 Or KeyAscii > 127 Then Exit Sub
If Combo1.SelLength = 0 Then
FindString = Combo1.Text & Chr$(KeyAscii)
Else
FindString = Left$(Combo1.Text, Combo1.SelStart) & Chr$(KeyAscii)
End If
CB = SendMessage(Combo1.hWnd, CB_FINDSTRING, -1, ByVal FindString)
If CB <> CB_ERR Then
Combo1.ListIndex = CB
Combo1.SelStart = Len(FindString)
Combo1.SelLength = Len(Combo1.Text) - Combo1.SelStart
End If
KeyAscii = 0
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 184 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.