ListBox - easy one

Hi
I have a pop up dialog box that contains a list box which displays a number of strings the user can select before clicking the OK button to close the dialog. How do I get and store the string the user selected?
Thanks
[233 byte] By [jaredino] at [2007-11-19 19:44:33]
# 1 Re: ListBox - easy one
Try this:

CString str;
int Index = m_list.GetCurSel();
if ( Index != LB_ERR )
m_list.GetText(Index,str);

Cheers
golanshahar at 2007-11-10 23:43:01 >
# 2 Re: ListBox - easy one
Maintain a cur sel member variable in the dialog box class , something like

class CYourDialog : public CDialog
{
...
public:
int m_nCurSel;
}

In OnOK handler, set it, as golanshahar suggests

void CYourDialog::nOK()
{
m_nCurSel = m_oListBox.GetCurSel();
CDialog::OnOk();
}

After DoModal, check the value like below

CYourDialog oDlg;
if(IDOK == oDlg.DoModal())
{
//oDlg.m_nCurSel
}
kirants at 2007-11-10 23:44:01 >
# 3 Re: ListBox - easy one
Set up a DDX variable to hold the currently selected string. Access it iafter the dialog closes.
GCDEF at 2007-11-10 23:45:07 >
# 4 Re: ListBox - easy one
Thanks for the help but I still can't get this to work:

BOOL CInstrumentDlg::OnInitDialog()
{
...
pLB = (CListBox*)GetDlgItem(IDC_LIST_INSTRUMENT);
pLB->InsertString(-1, "Drums");
pLB->InsertString(-1, "Electric Guitar");
pLB->InsertString(-1, "Acoustic Guiatr");
pLB->InsertString(-1, "Bass");
pLB->InsertString(-1, "Snare");
}

void CInstrumentDlg::OnOK()
{
//get selected instrument and store it in a String
CString m_nCursorSel;
m_nCursorSel = pLB->GetCurSel();

CDialog::OnOK();
}

What am I doing wrong?
Thanks
jaredino at 2007-11-10 23:46:02 >
# 5 Re: ListBox - easy one
Thanks for the help but I still can't get this to work:

BOOL CInstrumentDlg::OnInitDialog()
{
...
pLB = (CListBox*)GetDlgItem(IDC_LIST_INSTRUMENT);
pLB->InsertString(-1, "Drums");
pLB->InsertString(-1, "Electric Guitar");
pLB->InsertString(-1, "Acoustic Guiatr");
pLB->InsertString(-1, "Bass");
pLB->InsertString(-1, "Snare");
}

void CInstrumentDlg::OnOK()
{
//get selected instrument and store it in a String
CString m_nCursorSel;
m_nCursorSel = pLB->GetCurSel();

CDialog::OnOK();
}

What am I doing wrong?
Thanks

Aside from not using the DDX mechanism

please don't use the m_ prefix for local variable. That denotes a class member.

Also, if you're going to use Hungarian notation, use it properly. n denotes an integer. A variable beginning with m_n would be expected to be an int member of the class.

pLB->GetText(pLB->GetCurSel(), m_nCursorSel);
GCDEF at 2007-11-10 23:47:06 >