Windows messages to read the choices in a ListBox control

Started by nandagopal, May 20, 2009, 07:16 PM

Previous topic - Next topic

nandagopal

Use the SendMessage API function to send the LB_GETCOUNT message to the control and get the number of choices. Then for each choice send the LB_GETTEXTLEN message to see how long the choice is and use LB_GETTEXT to get the actual text.

This is as much an exercise in using API function as it is useful because you can easily loop through the ListBox's choices using ordinary VB code, too.


Private Sub Command1_Click()
Dim num As Long
Dim i As Integer
Dim txt As String
Dim entry As String
Dim length As Long

    ' See how many entries the list has.
    num = SendMessage(List1.hwnd, LB_GETCOUNT, 0, 0)
   
    ' Read each entry.
    For i = 0 To num - 1
        ' See how long the entry is.
        length = SendMessage(List1.hwnd, LB_GETTEXTLEN, i, _
            0)
       
        ' Make entry big enough.
        entry = Space$(length + 1)

        ' Get the entry.
        length = SendMessage(List1.hwnd, LB_GETTEXT, i, _
            ByVal entry)
        txt = txt & Left$(entry, length) & vbCrLf
    Next i
   
    MsgBox txt
End Sub