Listboxes and SendMessage API

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
Steve GronerLead Systems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Steve GronerLead Systems EngineerAuthor Commented:
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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Steve GronerLead Systems EngineerAuthor Commented:
Steve GronerLead Systems EngineerAuthor Commented:
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...
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

Steve GronerLead Systems EngineerAuthor Commented:
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

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.