简体   繁体   English

在Visual Studio MFC中,我可以在组合框中输入文本,但在代码中得到整数吗?

[英]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. 我将预填充的下拉菜单设置为MFC中的组合框。 The code needs a value between 0 and 15, but these values actually represent times. 代码需要一个介于0到15之间的值,但是这些值实际上代表时间。 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. 我可以对字符串值进行下拉,然后使用switch语句根据返回的字符串值选择int值,但这似乎已经内置了。

I'm quite new to MFC, mostly been stumbling my way through, so I may well have missed an obvious solution. 我对MFC还是很陌生,大部分时间都在绊脚石,所以我很可能错过了一个显而易见的解决方案。

You can use SetItemData to set an unsigned integer value for every item in the combo box. 您可以使用SetItemData为组合框中的每个项目设置无符号整数值。

The switch statement wouldn't work, since switch statements don't work on strings. switch语句不起作用,因为switch语句不适用于字符串。 You could use std::map or std::unordered_map though. 您可以使用std::mapstd::unordered_map

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM