How can I get the value (string) of the current selection in a combobox?
如何在组合框中获取当前选择的值(字符串)?
(Not the integer index)
(不是整数索引)
4 个解决方案
#1
There might be a better way (my MFC is a bit rusty), but it seems like you should be able to call CComboBox::GetLBText()
, passing it the current selection using CComboBox::GetCurSel()
.
可能有更好的方法(我的MFC有点生疏),但似乎你应该能够调用CComboBox :: GetLBText(),使用CComboBox :: GetCurSel()传递当前选择。
#2
A plain old GetWindowText
works, too.
一个普通的旧GetWindowText也可以工作。
Edit: As ajryan points out, GetWindowText
actually doesn't work in a CBN_SELCHANGE
handler, because the new selection has taken effect but the window text hasn't been updated with the text of the new selection when the WM_COMMAND
is sent.
编辑:正如ajryan所指出的,GetWindowText实际上在CBN_SELCHANGE处理程序中不起作用,因为新的选择已生效但在发送WM_COMMAND时窗口文本尚未使用新选择的文本进行更新。
#3
Use GetLBText, passing in the index and a CString object.
使用GetLBText,传入索引和CString对象。
edit: too slow!
编辑:太慢了!
#4
CB_GETCURSEL
return the integer index CB_GETLBTEXT
returns string at CB_GETCURSEL
CB_GETCURSEL返回整数索引CB_GETLBTEXT返回CB_GETCURSEL处的字符串
#1
There might be a better way (my MFC is a bit rusty), but it seems like you should be able to call CComboBox::GetLBText()
, passing it the current selection using CComboBox::GetCurSel()
.
可能有更好的方法(我的MFC有点生疏),但似乎你应该能够调用CComboBox :: GetLBText(),使用CComboBox :: GetCurSel()传递当前选择。
#2
A plain old GetWindowText
works, too.
一个普通的旧GetWindowText也可以工作。
Edit: As ajryan points out, GetWindowText
actually doesn't work in a CBN_SELCHANGE
handler, because the new selection has taken effect but the window text hasn't been updated with the text of the new selection when the WM_COMMAND
is sent.
编辑:正如ajryan所指出的,GetWindowText实际上在CBN_SELCHANGE处理程序中不起作用,因为新的选择已生效但在发送WM_COMMAND时窗口文本尚未使用新选择的文本进行更新。
#3
Use GetLBText, passing in the index and a CString object.
使用GetLBText,传入索引和CString对象。
edit: too slow!
编辑:太慢了!
#4
CB_GETCURSEL
return the integer index CB_GETLBTEXT
returns string at CB_GETCURSEL
CB_GETCURSEL返回整数索引CB_GETLBTEXT返回CB_GETCURSEL处的字符串