I have a CComboBox of type DropList
(i.e. it's not editable). What's the easiest way to set the current selection by string?
I know I can use SetCurSel()
to set it by index, but I want the function to search through the list-items by string and set it.
You can call FindStringExact() to obtain the index of the string you want to select, then pass that index to SetCurSel():
yourComboBox.SetCurSel(yourComboBox.FindStringExact(0, yourString));
Note that is the string is not found in the combobox, -1
will be passed to SetCurSel()
, which will result in any previous selection being cleared. You may want to perform an explicit test if that behavior does not suit you.
Note that Max's answer should be preferred for new developments. However, SelectString()
is only supported from Windows Server 2003 onwards, so you may not be able to leverage it, depending on the platforms you want to support.
What about CComboBox::SelectString ?
"Searches for a string in the list box of a combo box, and if the string is found, selects the string in the list box and copies it to the edit control."
https://msdn.microsoft.com/en-us/library/30ft9e54(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With