Finding a List Item
Now you will write an application that calls a DLL.
The example call the SendMessage function. SendMessage
enables you to send messages to controls to change
the control's appearance or get information back from
the control. In this example, you will use the SendMessage
to search a list box and return the index value of
the search string. The source code needed for this
example is stored in the "Source Code\Chapter24\Search"
directory.
Declare Function SendMessage Lib "user32"
_
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Any) As Long
Public Const LB_FINDSTRING = &H18F
Option Explicit
Private Sub Form_Activate()
txtSearch.SetFocus
End Sub
Private Sub Form_Load()
lstData.AddItem "One"
lstData.AddItem "Two"
lstData.AddItem "Three"
lstData.AddItem "Four"
lstData.AddItem "Five"
lstData.AddItem "Six"
lstData.AddItem "Seven"
lstData.AddItem "Eight"
lstData.AddItem "Nine"
lstData.AddItem "Ten"
End Sub
Private Sub txtSearch_Change()
Dim sTempStr As String
If lstData.ListCount < 1 Then Exit
Sub
sTempStr = txtSearch.Text
If Len(sTempStr) = 0 Then
lstData.ListIndex = 0
Else
lstData.ListIndex = SendMessage(lstData.hwnd,
_
LB_FINDSTRING, -1, sTempStr)
End If
End Sub
Run the application and enter any part of one of
the text strings that shows in the ListBox into the
TextBox. The ListBox cursor will automatically move
to number you enter.
Figure 24.2
|
|
In this chapter, you learned
how to use API routines that come with Windows.
Windows is a collection of dynamic link libraries
that contain thousands of routines you can access
from a Visual Basic program.
|
|