Jedi Wolf
Jedi Wolf

Reputation: 361

In Visual Studio MFC can I have text in a combo box but get an int in the code?

I have a prefilled drop down set up as a combobox in MFC. The code needs a value between 0 and 15, but these values actually represent times. Is there a way to have the display of the combo box show the times (strings) but still return the integer value for the spot?

I could make a dropdown of string values, then use a switch statement to choose the int value based of the returned string value, but this seems like the kind of thing that might be built in already.

I'm quite new to MFC, mostly been stumbling my way through, so I may well have missed an obvious solution.

Upvotes: 1

Views: 2284

Answers (2)

Sid
Sid

Reputation: 275

You need to convert the integer to string and then put in the combo box .

Put the following code in the OnInitDialog function



CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBO);
CWnd* pComboEdit = pCombo->GetWindow(GW_CHILD);
if (pComboEdit != NULL)
    pComboEdit->ModifyStyle(0, ES_NUMBER);
((CEdit*)pComboEdit)->LimitText(2);

CString str;
for (int i = 0; i <= 12; i++)
{
    str.Format(L"%d", i);
    pCombo->AddString(str);
}

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308101

You can use SetItemData to set an unsigned integer value for every item in the combo box.

The switch statement wouldn't work, since switch statements don't work on strings. You could use std::map or std::unordered_map though.

Upvotes: 4

Related Questions