VB and VBA Users Source Code: Search a listbox for a string
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Search a listbox for a string
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, February 12, 2001
Hits:
636
Category:
Windows API
Article:
To search a listbox for a string use the following routine. Note, demonstration code can be found at the bottom of this post. Option Explicit Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long 'Purpose : Searchs a listbox for a matching string 'Inputs : sSearchString The string to search the listbox items for ' lHwndListbox The handle to the listbox 'Outputs : Returns the listindex of the matching item 'Author : Andrew Baker 'Date : 10/02/2001 15:50 'Notes : 'Revisions : 'Assumptions : Function ListboxFindString(sSearchString As String, lHwndListbox As Long) As Long Const LB_FINDSTRING = &H18F ListboxFindString = SendMessage(lHwndListbox , LB_FINDSTRING, -1, ByVal sSearchString) End Function 'Demonstration routines. Add the relevant controls to the form 'and the user types in the text box the listbox should select 'the closest matching item Private Sub Text1_Change() Me.List1.ListIndex = ListboxFindString(Me.Text1, Me.List1.hWnd) End Sub Private Sub Form_Load() With List1 .AddItem "Andrew" .AddItem "Jim" .AddItem "Philip" .AddItem "Simon" .AddItem "Peter" .AddItem "Mark" .AddItem "Steven" End With End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder