The CB_GETCURSEL message finds out which list-box item is currently selected in a combo box control. This message only returns useful information if the combo box's selection is one of the items available in its list box portion. If the user types different data into the tex box portion, the function merely reports that no item is selected.
The message returns the zero-based index of the selected item (the first item has an index of 0, etc.). If none of the items in the list box portion are selected, the message returns -1.
None.
Const CB_GETCURSEL = &H147
' 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 CB_GETCURSEL = &H147
Public Const CB_GETLBTEXT = &H148
Public Const CB_GETLBTEXTLEN = &H149
' Display the text of whatever item in combo box Combo1
' is currently selected. If no list box item is selected, say so.
Dim index As Long ' index to the selected item
Dim itemtext As String ' the text of the selected item
Dim textlen As Long ' the length of the selected item's text
' Determine the index of the selected item.
index = SendMessage(Combo1.hWnd, CB_GETCURSEL, ByVal CLng(0), ByVal CLng(0))
' Decide what to do based on that.
Select Case index
Case -1 ' No list box item was selected.
Debug.Print "No list box item in the combo box is selected."
Case Else ' Some item is selected.
' Determine how long the item's text is.
textlen = SendMessage(Combo1.hWnd, CB_GETLBTEXTLEN, ByVal CLng(index), ByVal CLng(0))
' Make enough room in the string to receive the text, including the terminating null.
itemtext = Space(textlen) & vbNullChar
' Retrieve that item's text and display it.
textlen = SendMessage(Combo1.hWnd, CB_GETLBTEXT, ByVal CLng(index), ByVal itemtext)
itemtext = Left(itemtext, textlen)
Debug.Print "Selected item: "; itemtext
End Select
Back to the Message list.
Back to the Reference section.
Last Modified: April 16, 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/c/cb_getcursel.html