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!

Selecting a value from a Combo Box

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I cannot select a value form a combo box, the Combox is populated by using a for loop with a count. I only manage to get the last value each time.
 
Hi

Here is the two ways to get/set comboboxes:

I set up a dialogbox with a combo ( IDC_COMBO) and use the ClassWizard to add two members:

one to type string m_StrCombo
one of type control m_Combo

The ClassWizard adds this in DoDataExchange

DDX_Control(pDX, IDC_COMBO, m_Combo);
DDX_CBString(pDX, IDC_COMBO, m_strCombo);

I populate the combo in OnInitDialog:

CString str;

for ( int n = 0; n < 10; n++)
{
str.Format( &quot;%d&quot;, n);

m_Combo.AddString( str);
}

Here is the easy way to get and set:

to get the currently selected item:

UpdateData( TRUE);

AfxMessageBox( m_strCombo);

to set a new selection:

m_strCombo = &quot;1&quot;; // this set the second item
// item 0 is &quot;0&quot;

UpdateData( FALSE);

The 'hard' way:

to get the current selection

CString str;

int nItem = m_Combo.GetCurSel();

if ( nItem != CB_ERR)
{
m_Combo.GetLBText( nItem, str);

AfxMessageBox( str);
}

to set a new selection

m_Combo.SetCurSel( 2); // this set the third item

HTH

Thierry
EMail: Thierry.Marneffe@swing.be

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top