The LB_GETTEXT message retrieves the text of one of the items in a list box control.
If successful, the message returns the length of the string copied into the string passed as lParam, not including the terminating null character. If an error occured, the message returns -1.
None.
Const LB_GETTEXT = &H189
Display the text of the second-to-last item in list box List1 when the user clicks button Command1. Obviously, to use this example you must place a list box named List1 and a command button named Command1 on a form window.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd _
As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Public Const LB_GETCOUNT = &H18B
Public Const LB_GETTEXT = &H189
Public Const LB_GETTEXTLEN = &H18A
' *** Place the following code inside a form window. ***
Private Sub Command1_Click()
Dim count As Long ' number of items in the list box
Dim s2l As Long ' index of the second-to-last item
Dim itemtext As String ' text of that item
Dim textlen As Long ' length of the item text
' Figure out the index of the second-to-last item by subtracting two from
' the total item count (remember, the index is zero-based).
count = SendMessage(List1.hWnd, LB_GETCOUNT, ByVal CLng(0), ByVal CLng(0))
s2l = count - 2
' Make the string long enough to receive that item's text.
textlen = SendMessage(List1.hWnd, LB_GETTEXTLEN, ByVal s2l, ByVal CLng(0))
itemtext = Space(textlen) & vbNullChar
' Get the item text and remove the trailing null.
textlen = SendMessage(List1.hWnd, LB_GETTEXT, ByVal s2l, ByVal itemtext)
itemtext = Left(itemtext, textlen)
' Finally, display the result.
Debug.Print "The second-to-last item is "; itemtext
End Sub
Back to the Message list.
Back to the Reference section.
Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/l/lb_gettext.html