How to capture the selected text in a ListBox....

Started by
4 comments, last by OOBradm 21 years, 4 months ago
CListBox *List; List = (CListBox*)GetDlgItem(IDC_LIST); List->???????; That is the code I have....I need to get the text of the selected option in the list box. GetWindowText(); seemed like the obvious choice but that didnt work. Does Anyone know the function? Thanks in advance, -Brad
Advertisement
hehehe, the first time I got that problem...........

Ok, do the following:

  TCHAR SelectedText[256];int nCurSel = SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);SendDlgItemMessage(hWnd, IDC_LIST, LB_GETTEXT, (WPARAM) nCurSel, (LPARAM) (LPCTSTR) SelectedText);  


To find the list of commands available for ListBoxes search
for "List NEAR Box NEAR Messages" in the MSDN Library.

Kamikaze
Hey, I got it to work...thanks a lot. Just for my knowledge, is that the ONLY way to get the selected text from a listbox? There isnt just a function that would return it? ty again,
-Brad

[edited by - OOBradm on December 2, 2002 2:42:48 PM]
tried to look at CListBox member functions?
Why don't you create a function that returns the
selected text like:


      void GetSelectedText( HWND hWnd, UINT ResourceID, TCHAR *String ){   TCHAR SelectedText[256];   int nCurSel = SendDlgItemMessage(hWnd, IDC_LIST, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);   SendDlgItemMessage(hWnd, ResourceID, LB_GETTEXT, (WPARAM) nCurSel, (LPARAM) (LPCTSTR) SelectedText);     lstrcpy( String, SelectedText );}  


Hope that helps

Kamikaze

[edited by - Kamikaze15 on December 2, 2002 6:18:32 PM]

[edited by - Kamikaze15 on December 2, 2002 6:20:02 PM]
function prototypes:
int GetCurSel();
void GetText(int nIndex, CString &rString);

example:
int Index;CString MyString;...Index = List->GetCurSel();if(Index != -1){  List->GetText(Index, MyString);} 

Use Kamakazi's way for non-MFC apps.


[edited by - Hollower on December 2, 2002 6:52:49 PM]

This topic is closed to new replies.

Advertisement