c++mfcclistbox

Why can't I get the selected items in the CListBox? MFC


I have an MFC dialog with a CListBox on it. I'm able to successfully add items to it using CListBox::AddString and set data pointers using CListBox::SetItemDataPtr. The strings show in the listbox and I can retrieve the data pointers by calling CListBox::GetItemDataPtr and passing in a static index. I'm not able to the get the selected index using ClistBox::GetSelItems. I followed the example on msdn: http://msdn.microsoft.com/en-us/library/ds24bscf.aspx and even tried using LPINT:

LPINT selItem = new int;
m_Lb_Avail_Cmnds.GetSelItems(1, selItem); //m_Lb_Avail_Cmnds is a CListBox
int i = *selItem;

But it's always returning 0 despite which item is selected.


Solution

  • In a single-selection listbox (that I am guessing you're using because you call GetSelItems with nMaxItems set to 1), GetSelItems is not supported. Use GetCurSel instead:

    int i = m_Lb_Avail_Cmnds.GetCurSel();