Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const LB_FINDSTRING = &H18F
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Sub Form_Load()
'add this code to the form load to
'fill a list with some (15) of the
'system's screen fonts.
Dim i As Integer
Dim max As Integer
max = Screen.FontCount
If max > 50 Then max = 50
For i = 1 To max
List1.AddItem Screen.Fonts(i)
Next
End Sub
Private Sub Text1_Change()
On Error Resume Next
'find the first match beginning at the
'start of the list. To specify searching
'should begin with a specific list item,
'change wParam from -1 to a value
'between 0 and List1.ListCount -1.
List1.ListIndex = SendMessage(List1.hwnd, _
LB_FINDSTRING, _
-1, _
ByVal (Text1.Text))
End Sub
|
Run the project, and begin to type a font name into the
textbox. If there is a font beginning with the entered text, it will be selected.
The return value of the call is the ListIndex of the matching item,
or -1 if a match is not found. Remember too that whenever code sets the ListIndex property, it causes a List_Click event to fire. If this is
undesirable, appropriate flags need to be implemented.
If the constant used in the API call is LB_FINDSTRING, any partial
match *beginning* with the entered string is located. If the constant LB_FINDSTRINGEXACT is used, then
the entered text much match exactly the string in the listbox.
LB_FINDSTRINGEXACT is most useful when attempting to determine if the
precise string specified is in a list.
Passing Text1.Text in brackets causes the variant data containing the
text property to be coerced into a string data type for passing ByVal
without error. If Text1.Text is cast to a string variable instead, the
brackets around the variable are unneeded, however the parameter still
must be passed to the API ByVal.
|