Listboxes and SendMessage API

Posted on 1998-03-13
Last Modified: 2012-05-04
Can somone send me some sample code to allow me to additems and itemdata to a listbox using the SendMessage API call.  I would like to add the item, and item data in the same loop, can someone help me.  I need this urgently.  Thanks
Question by:Steve Groner
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
  • 4
  • 4

Expert Comment

ID: 1459088
Declare Sub CopyMemory Lib "user32" Alias "RtlMoveMemory" (Source As Any, Dest As Any, size As Long)
Dim p as long
Dim x as long

CopyMemory p, "Item1", len(Item1)
X = SendMessage(List1.hwnd, LB_ADDITEM, 0, p)
SendMessage List1.hwnd, LB_SETITEMDATA, X, value

you can do the same in vb, but much more easier:
list1.additem "item"
list1.itemdata(list1.newindex) = value

Author Comment

by:Steve Groner
ID: 1459089
Not the code I am looking for.  I was told that when adding alot of record to a list box, like 10,00 plus items that using sendmessage would be faster.  MrMick had answered a question like this previously, but it was only for the adding itemdata.  This fuction should only use the sendmessage command, also do I need to set the redraw to off until it is completed.  I know I am a little off on the original question but I will upgrade the points to 100.

Accepted Solution

MikeP090797 earned 100 total points
ID: 1459090
To speed up the adding items to the list call SendMessage with LB_INITSTORAGE:
SendMessage(list1.hwnd, LB_INITSTORAGE, 10000, 200000)
The answer i provided is good for adding items and setting itemdata. To prevent the redraw, use LockWindowUpdate
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

by:Steve Groner
ID: 1459091

Author Comment

by:Steve Groner
ID: 1459092
MikeP, the rtlmovememory function does not exist, perhaps you can tell me wher eit is, I tried the code exactly as you have it and it does not work.  Help...

Expert Comment

ID: 1459093
Sorry, the correct declare is:
Private Declare Function MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, src As Any, size As Long) As Long

BTW: It won't make any differense if you are using .AddItem or LB_ADDSTRING, cause.AddItem calls directly LB_ADDSTRING. The important part of the optimization is LB_INITSTORAGE and disabling the redresh


Author Comment

by:Steve Groner
ID: 1459094
MikeP did you test this code before you sent it.  When I use this code now, it acts as though it is doing something but nothing ever appears in the list, how do you turn on and off the refresh.  I do not know.  Here is the code I am using.

    Dim sSQL As String
    Dim rs As rdoResultset
    Dim retcode As Long
    Dim p As Long
    Dim sName As String
    sSQL = "Select * from apps ORDER BY LastName;"
    Set rs = cn.OpenResultset(sSQL, rdOpenDynamic, rdConcurRowVer)
    SendMessage list1.hwnd, LB_INITSTORAGE, 10000, 200000

    Do Until rs.EOF
        sName = rs!LastName + ", " + rs!FirstName
        MoveMemory p, sName, Len(sName)
        retcode = SendMessage(list1.hwnd, LB_ADDSTRING, 0, p)
        SendMessage list1.hwnd, LB_SETITEMDATA, retcode, CLng(rs!Unique)

    Set rs = Nothing


Expert Comment

ID: 1459095
You lock the window update when you pass the listbox handle, and unlcok it when you pass null:

LockWindowUpdate(List1.hwnd)  'Lock
LockWindowUpdate(vbNull)      'Unlock

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

752 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