I have an alphabetically sorted combobox in a dialog. This combo contains multiple strings, but some are duplicated with different cases. i.e. we have an 'On' and an 'ON', an 'Off' and an 'OFF'. This may seem redundant but there is a reason, although this is not important right now.
The duplicates obviously appear one after the other in the list, with the capitalized strings first. i.e.:
OFF
Off
ON
On
When the user selects the 'On' (lower case), the correct index is set as CurSel
and the correct string is displayed. However, when I click on the arrow of the combobox to drop down the list, it does not highlight the CurSel
, but the one previous to it, the capitalized string. See images below.
This is was is selected in the dropdown:
This is what is selected in the combobox when expanding the dropdown.
I have captured the ON_CBN_DROPDOWN
message, and checked the cursel value and it is as I expected.
I have also already subclassed this combobox so that I can search for strings in this list in a case-sensitive way, as I know its not implemented normally, so it may be what is causing my issue.
But I don't understand why the string would be overriding the cursel value at this stage? Should the CurSel
value not be the one used to select the relevant item?
Any ideas on how I can fix this would be greatly appreciated.
EDIT:
I have tried to capture the CBN_DROPDOWN
message by overwriting the OnWndMsg
. When this message occurs, I get the currently selected item (which is the correct item) before dropping down the menu. I then drop the menu, and call SetCurSel
to what I retrieved before.
BOOL CMyComboBox::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT *pResult)
{
if(message == CBN_DROPDOWN)
{
int nCurSel = GetCurSel();
if(nCurSel != CB_ERR)
{
ShowDropDown();
SetCurSel(nCurSel);
return TRUE;
}
}
return CComboBox::OnWndMsg(message, wParam, lParam, pResult);
}
This kind of works but when I kill focus, or click on the dropdown arrow again to hide the dropdown, the wrong item is displayed in the text box. Is this a valid method, or am I completely off base here? What message is sent when the drop down is collapsed?
EDIT 2: I have implemented the case-sensitive combobox from code project and it works great.