• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Setting Object Data *double* For Class ComboBox :: MFC

kuphryn

Senior member
Hi.

I have a CComboBox object. I need to set one or more of its strings to represent a *double* data type. For example:

Code:
CComboBox mCbbCandy;

mCbbCandy.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY | WS_VSCROLL | CBS_DROPDOWNLIST | CBS_SORT, CRect(CPoint(270, 10), CSize(100, 100)), this, IDC_CBB_FLAVORS);

// Setting its value
int i = mCbbCandy.AddString("X");
mCbbCandy.SetItemData(i, 1.99); // $1.99

// Retrieving its value

int i = mCbbCandy.GetCurSel();
double price = mCbbCandy.GetItemData(i);

-----

For some reason, the price does not come out right if I use the decimal point. For example, any whole number works, but not decimal. 

How do you set the item data to a double type? Visual C++ gave warnings about data loss because the CComboBox object expected a DWORD instead of a double.

Thanks,
Kuphryn
 
just insert the number as a string:

TCHAR szNumber[10] = {0};

wsprintf(szNumber, "%f", 1.99);
mCbbCandy.AddString(szNumber);

The function you were using SetItemData, is used to set a pointer for reference (kind of like the lparam in tree/list views) so its not at all meant to set the value of the combo box item. Hope that helps
 
javathehut's suggestion will work *unless* you want what the combo box to display to be different from the data that you retrieve from it. If so, you can do a little hack to get your data stored and retrieved properly:

Do a memcpy to copy your float to a DWORD variable, then use SetItemData to store your DWORD. When you need to retrieve the float, do the opposite - use GetItemData to get the DWORD, then memcpy it back to the float. Both DWORD and float are 4 bytes, so memcpy will work.


Hope that helps,
🙂atwl


Edit:
Ignore my first suggestion, I just realized you want a double, not a float. A double is 8 bytes, so there's nothing that you can do except for javathehut's suggestion. Unless you use a pointer to a structure/array/list that stores your double, that is.
 
Back
Top