Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox

Status
Not open for further replies.

Jaredino

Technical User
Jan 31, 2006
17
GB
Hi,
I've tried a number of things but just can't get this to work. I have a list box containing a number of strings:

Code:
void CInstrumentDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInstrumentDlg)
	DDX_LBString(pDX, IDC_LIST_INSTRUMENT, m_CursorSel);
	//}}AFX_DATA_MAP
}
BOOL CInstrumentDlg::OnInitDialog() 
{
	pnListBox = (CListBox*) GetDlgItem (IDC_LIST_INSTRUMENT);
	pnListBox->InsertString(-1, "Drums");
	pnListBox->InsertString(-1, "Electric Guitar");
	pnListBox->InsertString(-1, "Acoustic Guitar");
	pnListBox->InsertString(-1, "Bass");
	pnListBox->InsertString(-1, "Snare");

	return CDialog::OnInitDialog();	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

All I want to be able to do is to store the selected string in a CString variable, which I tried doing like this:

Code:
void CInstrumentDlg::OnOK() 
{
	pnListBox->GetText(pnListBox->GetCurSel(), m_CursorSel);
	CDialog::OnOK();
}

CString CInstrumentDlg::getString()
{
	return m_CursorSel;
}

The getString function could then be called to show the selected string somewhere else in my program.
What am I not understanding?

Thanks in advance
 
Do you wish to access the string after the dialog has been closed and deleted?
 
Yeah, so the idea is that the variable m_CursorSel contains the string say "Drums" until the user clicks the toolbar button again to reopen the dialog. The m_CursorSel will be assigned he newly selected string say "Bass" or even "Drums" again.
It might even be good to store the list of selections in the order they were chosen say:
1. Drums
2. Bass
3. Acoustic Guitar

Thanks
 
Two ways
1) Make m_CursorSel static. This doesn't work if you can have multiple copies of the form up, each one selecting a different item from the listbox.
2) After your DoModal, before deleting the dialog, save the string to somewhere more permanent.
 
Thanks, I used the second way. I forgot that the dialog, and therefore the string would be deleted once the dialog closed!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top