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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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