最近更新于 2025-03-05 00:07
测试环境
使用示例
创建一个基于对话框的新项目 comboBoxProject
删除所有默认的控件,添加一个 Combo Box
在Combo Box上右键添加变量,创建一个 private 变量 comboBoxControl
在项目 Dlg 类的 OnInitDialog 方法中添加用于初始化对话框的代码
// 添加项目
this->comboBoxControl.AddString(L"苹果");
this->comboBoxControl.AddString(L"香蕉");
// 设置默认选项
this->comboBoxControl.SetCurSel(0);
// 在指定索引位置插入
this->comboBoxControl.InsertString(1, L"橙子");
// 删除指定索引位置的选项
this->comboBoxControl.DeleteString(2);
// 获取指定索引的选项
CString itemString;
this->comboBoxControl.GetLBText(1, itemString);
MessageBox(itemString);
运行
属性,添加一个事件回调,这个事件在选中选发生修改时触发
// 获取当前选项的索引
int idx = this->comboBoxControl.GetCurSel();
CString currentItemString;
this->comboBoxControl.GetLBText(idx, currentItemString);
MessageBox(currentItemString);
这样修改选中项的时候就会弹出新的选中项
MFC:Combo Box 组合框