c++user-interfacemfc

Can I remove items from a ComboBox without changing the index of other items?


I have a CComboBox control with several items and I need to remove some of them, but the indexes of the remaining items should be preserved.

When the combo box is populated, the item data is set like so:

index = mycombo.AddString(temp);
mycombo.SetItemData(index, static_cast<DWORD>(count));

where count is a loop counter, and should be equal to index

Now I want to remove some of the items later, but I need the index of each remaining item to stay the same. Is CComboBox::DeleteString(UINT nIndex) what I should use? Its documentation says:

All items following nIndex now move down one position. For example, if a combo box contains two items, deleting the first item will cause the remaining item to now be in the first position. nIndex=0 for the item in the first position.

Is that talking about the physical location in the drop-down menu, or the index value associated with the item?

Is there another function that does what I need? Another solution altogether?


Solution

  • Adding or removing items does not change the number you passed to SetItemData(). GetItemData() returns the same number. You however need to pass the index of the item to DeleteString(). When lower numbered items were deleted before then the index will no longer match GetItemData(). If you lost track of the index of a specific item you want to delete then you need to iterate the items to find it back.