MFC ComboBox 使用方法-初始化,添加,得到选择的数据,得到数据的位置,删除
来源:互联网 发布:软件测试管理岗位职责 编辑:程序博客网 时间:2024/04/29 18:33
Combo box controls are space savers. Wherever there is no need for a multi-select from a list of items, combo box is a good choice in such places. This article " CComboBox Example" explains how to use the MFC CComboBox class for manipulation of a list of strings.
CComboBox Example - Initializing a Combo Box:
It is assumed that the readers of the sample have already created a dialog box (either in a dialog based application or SDI/MDI application) and placed a combo box control from the controls toolbox on the Resource Editor.
After placing the combo box control on the dialog box, open the class wizard by pressing Ctrl + W keys or Menu --> View --> ClassWizard. In the Member Variables tab, Add a Variable for the CComboBox class. This CComboBox example assumes that the variable name is,
CComboBox m_cbExample;
This m_cbExample will be used further in our CComboBox example MFC code.
CComboBox Example - Adding Items to a Combo Box:
The function AddString is used for adding items to a combo box. If there is a constant set of data, these values can also be added in the Resource Editor itself. The Combo Box control properties dialog has a tab for adding data. Otherwise the data can be added as follows.
m_cbExample.AddString("StringData1");
m_cbExample.AddString("StringData2");
m_cbExample.AddString("StringData3");
CComboBox Example - Retrieving Items from a Combo Box:
Usually
a requirement for retrieving items from the combo box will arise from selecting the data. This article also assumes the same. Now the data selected in a combo box needs to be retrieved.
To do this, the first step is to find out the index of the selected item inside the combo box control.
Then the item at the corresponding position needs to be pulled out as follows.
int nIndex = m_cbExample.GetCurSel();
CString strCBText;
m_cbExample.GetLBText(
nIndex, strCBText);
In the above CComboBox example code, the value will be retrieved and stored in strCBText variable. There is another overloaded version for GetLBText. But the version which uses CString is the easiest one.
CComboBox Example - Finding Items inside a Combo Box:
This kind of Find operations on a Combo box will most probably be useful in programs that dynamically modify the values in a combo box. The function FindStringExact is used to find the exact string match inside a combo box.
int nIndex = m_cbExample.FindStringExact(0, "Value to be found");
The string position inside the combo box control is the return value. It returns CB_ERR if it was unsuccessful in finding the string.
CComboBox Example - Deleting Items from a Combo Box:
This operation can be done by using the CCombobox member function DeleteString. This function needs the index of the item inside the combo box.
m_cbExample
.DeleteString(nIndex);
- MFC ComboBox 使用方法-初始化,添加,得到选择的数据,得到数据的位置,删除
- 如何得到类数据成员的位置
- 如何得到类数据成员的位置
- 得到服务器的数据
- 如何得到ClistBox当前选择的内容,以及如何初始化CListBox.删除当前选择的内容
- C#之WinForm基础 得到comboBox的选择的内容
- oracle 得到本周的数据
- Android Json数据常见的得到数据
- combox从数据库获取到数据,并且选择的时候得到数据,而不是NULL。
- 得到组织机构的上下级的数据
- (MFC)CTreeCtrl得到当前选择项的方法
- 得到某个数据的前N行
- php5.2无法得到POST的数据
- 得到公司代码的详细数据
- php5.2无法得到POST的数据
- 不能得到位图数据的一个原因
- C++解析得到的JSON数据
- SQL汇总查询得到的数据
- DirectShow 在vs2005中的问题解决之应用在ACTIVEX中
- String和StringBuilder
- typedef struct与struct的区别
- mysql命令学习二
- VB也是支持多线程
- MFC ComboBox 使用方法-初始化,添加,得到选择的数据,得到数据的位置,删除
- ubuntu上mount ext3的磁盘,除了root用户外其他用户不能读写文件的解决方法
- 【听大师说话4】PHP创始人 Rasmus Lerdorf 访谈 1
- 我就是一个香港导演---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三十八)
- 图片在IE下与容器下边界有空隙的解决方法
- 怎样成为优秀的软件工程师【转】
- 要知道偏见伤人
- 分享实用工具源码--实现Windows IDE中查看Linux下编译信息
- 只有通过不断地构建和巩固,人际关系才能牢固