Results 1 to 3 of 3

Thread: LB_GETTEXT, does it work?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421

    LB_GETTEXT, does it work?

    I've tried many times trying to get ALL the text from a ListBox. But nothing seems to work, I've tried searching through posts and stuff, but nobody seems to want what I need. How can I get all of the text from a ListBox, and put it in a TextBox? Example:

    The ListBox looks like this:

    Dog
    Cat
    Cow
    Horse

    and when you press a button, the TextBox looks like this:

    Dog
    Cat
    Cow
    Horse

    Anyone know how to do this??
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    In a Module:
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2.  
    3. Private Const LB_GETCOUNT = &H18B
    4. Private Const LB_GETTEXT = &H189
    5. Private Const LB_GETTEXTLEN = &H18A
    6.  
    7. Public Function GetListBoxItems(ByVal hWnd As Long) As String
    8.     Dim lCount As Long, lIndex As Long
    9.     Dim sItem As String, lLen As Long
    10.     Dim sList As String
    11.    
    12.     ' Find the no. of items in the listbox
    13.     lCount = SendMessage(hWnd, LB_GETCOUNT, 0&, ByVal 0&)
    14.     ' Enumerate the items
    15.     For lIndex = 0 To lCount - 1
    16.         ' Get the length of the Item Text
    17.         lLen = SendMessage(hWnd, LB_GETTEXTLEN, lIndex, ByVal 0&)
    18.         ' Create a buffer of the specified length
    19.         sItem = Space(lLen)
    20.         ' Extract the Item Text into the buffer
    21.         Call SendMessage(hWnd, LB_GETTEXT, lIndex, ByVal sItem)
    22.         ' Add it to the string
    23.         sList = sList & vbCrLf & sItem
    24.     Next
    25.     ' Return the string minus the first preceeding CRLF
    26.     GetListBoxItems = Mid(sList, 3)
    27. End Function
    Example Usage:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim lIndex As Long
    3.     For lIndex = 1 To 10
    4.         List1.AddItem "Item " & lIndex
    5.     Next
    6. End Sub
    7.  
    8. Private Sub Command1_Click()
    9.     ' Using Local Listbox for example, but could pass the Handle of any external Listbox
    10.     Text1 = GetListBoxItems(List1.hWnd)
    11. End Sub

  3. #3
    Hyperactive Member Ed Lampman's Avatar
    Join Date
    Mar 2001
    Posts
    273
    Private Sub Command1_Click()

    Dim i as integer
    Dim s as String

    for i = 0 to List1.Listcount-1
    s=s & List1.List(i) & vbNewLine
    next

    Text1.Text = s

    end sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width