VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



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

by Omar Vivas (4 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (9 Votes)

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

Inputs
The String to find.
Assumes
You must set the API Declaration in a Module
Code 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

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

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

Download this snippet    Add to My Saved Code

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

No comments have been posted about Find faster a String in Combo or ListBox while typing (Using SendMessage API). Why not be the first to post a comment about Find faster a String in Combo or ListBox while typing (Using SendMessage API).

Post your comment

Subject:
Message:
0/1000 characters