在 CComboBox (MFC) 中通过文本设置选择

Setting selection by text in CComboBox (MFC)

我有一个 DropList 类型的 CComboBox(即不可编辑)。通过字符串设置当前选择的最简单方法是什么?

我知道我可以使用 SetCurSel() 按索引设置它,但我希望函数按字符串搜索列表项并设置它。

您可以拨打FindStringExact() to obtain the index of the string you want to select, then pass that index to SetCurSel():

yourComboBox.SetCurSel(yourComboBox.FindStringExact(0, yourString));

请注意,如果在组合框中找不到该字符串,-1 将传递给 SetCurSel(),这将导致清除之前的任何选择。如果该行为不适合您,您可能想要执行显式测试。

请注意, 应优先用于新开发。但是,SelectString() 仅从 Windows Server 2003 开始​​受支持,因此您可能无法利用它,具体取决于您要支持的平台。

那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