Sendmessage API function

Posted on 2003-03-13
Medium Priority
Last Modified: 2008-02-01
i have a textbox and listbox, so whenever textbox change event occur the SendmessageAPI function comes into play, the the item in the listbox willbe selected, now i have 2 item in the listbox :
BANDIT,BUDDY when i type character B in the textbox the first selected item is BUDDY which according to the sequence of the alphabet is not true, by right it supposed to select BANDIT since BA comes first and not BU,thank you

THIS IS THE api i use :
Public 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_SELECTSTRING = &H18C
Public Const Namadatabase = "C:\MPNVB6.0\NM\OPERATOR108.mdb"

Private Sub txtfind_Change()
Dim retval As Long
Dim findword As String
cari = txtfind.Text
retval = SendMessage(List1.hwnd, LB_SELECTSTRING, _
0, findword)

End Sub
Question by:indra_putra
LVL 39

Accepted Solution

appari earned 200 total points
ID: 8133093
just replace
retval = SendMessage(List1.hwnd, LB_SELECTSTRING, _
0, findword)
retval = SendMessage(List1.hwnd, LB_SELECTSTRING, _
-1, findword)

reason(From MSDN):

An application sends an LB_SELECTSTRING message to search a list box for an item that begins with the characters in a specified string. If a matching item is found, the item is selected.


To send this message, call the SendMessage function as follows.
lResult = SendMessage(      // returns LRESULT in lResult     (HWND) hWndControl,      // handle to destination control     (UINT) LB_SELECTSTRING,      // message ID     (WPARAM) wParam,      // = (WPARAM) () wParam;    (LPARAM) lParam      // = (LPARAM) () lParam; );  

Specifies the zero-based index of the item before the first item to be searched. When the search reaches the bottom of the list box, it continues from the top of the list box back to the item specified by the wParam parameter. If wParam is –1, the entire list box is searched from the beginning.
Microsoft® Windows® 95/Windows 98/Windows Millennium Edition (Windows Me) : The wParam parameter is limited to 16-bit values. This means list boxes cannot contain more than 32,767 items. Although the number of items is restricted, the total size in bytes of the items in a list box is limited only by available memory.

Pointer to the null-terminated string that contains the prefix for which to search. The search is case independent, so this string can contain any combination of uppercase and lowercase letters.

LVL 11

Expert Comment

ID: 8133114
I am not sure what exactly you want.  here is a code without using the API.

Private Sub txtfind_Change()
     Dim retval As Long

        if (Trim(txtFind.Text) <> "") then
             FindInList Trim(txtFind.Text)
     End if
End Sub

Private Sub FindInList(str as String)
     Dim LastStr As String
     Dim LastIndex as long
     Dim i as long
        LastStr = "ZZZZZZ"
     LastIndex = -1
     With ListItems
          For i = 0 to .ListCount-1
               if (UCase(.List(i)) >= UCase(str) And UCase(.List(i)) < UCase(LastStr)) then
                    LastStr = .List(i)
                    LastIndex = 0
               End if
          Next i
          .ListIndex = LastIndex
     End With
End Sub

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question