MFC ComboBox 使用方法
2007-05-26 09:41
531 查看
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 - 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.
相关文章推荐
- MFC ComboBox 使用方法
- MFC ComboBox 使用方法
- MFC使用ComboBox控件方法
- 在vs2008里使用comboBox编写MFC程序 向comboBox添加数据方法
- MFC ComboBox 使用方法-初始化,添加,得到选择的数据,得到数据的位置,删除
- MFC ComboBox 使用方法
- MFC ComboBox 控件使用方法
- MFC ComboBox 使用方法(转载)
- MFC ComboBox 使用方法(转载)
- 引用 MFC ComboBox 使用方法
- MFC ComboBox 使用方法
- MFC ComboBox 使用方法
- MFC ComboBox 使用方法
- MFC下使用CvvImage在子对话框(非主对话框)中无法显示图片的解决方法
- 关于MFC项目中使用CDHtmlDialog禁止脚本错误的方法
- 使用OpenCv库的MFC程序在退出后报内存泄露Detected memory leaks! 可能解决方法
- EasyUI中combobox的使用方法
- MFC中使用CUDA5.0的方法(VS2010环境)
- MFC ComboBox的使用
- MFC中Radio Button使用方法