Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleUse Windows messages to read the choices in a ListBox control
DescriptionThis example shows how to use Windows messages to read the choices in a ListBox control in Visual Basic 6. This is more an exercise in using API functions than something you really need to do.
KeywordsListBox, API, items
CategoriesControls
 
The program uses the SendMessage API function to send the LB_GETCOUNT message to the ListBox. SendMessage returns the number of items in the list.

For each item, the program sends the control the LB_GETTEXTLEN message to get the item's length. It allocates space in a string for the item and then sends the LB_GETTEXT message to copy the item into the string. Notice that the program then uses the Left$ function to get the item's text. This removes the trailing Null character from the string.

 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated