MFC 中 CComboBox常用的使用方法


在工具栏中添加 CComboBox,属性:Type有三种格式【DropDown】【Simple】【Drop List】,

初始化下拉框数据:

for(int xh=0;xh<100;xh++){
		//item="Item"+xh;
		char item[256];
		sprintf(item,"Item%d",xh);
		int i= this->IDC_DropDown.AddString(item); //添加数据

		this->IDC_DropDown.SetItemData(i,1000+xh); //添加附加的数据 这里的第一个参数,一定要是 AddString之后的返回结果,否则会获取不到附加值信息,已经测试过

		this->IDC_Simple.AddString(item);
		this->IDC_DropList.AddString(item);
	}

获取当前下拉框选中的索引:

this->IDC_DropDown.GetCurSel()

获取选中的文本:

String curText;
	this->IDC_DropDown.GetLBText(this->IDC_DropDown.GetCurSel(),curText); //获取选中的数据
	AfxMessageBox(curText);

删除选中的对象:

this->IDC_DropDown.DeleteString(this->IDC_DropDown.GetCurSel()); //删除数据

获取附加的数据:

int selectIndex = (this->IDC_DropDown.GetCurSel());
	CString sdata;
	sdata.Format("附件数据序号:%d",selectIndex);
	AfxMessageBox(sdata);
	DWORD_PTR text = this->IDC_DropDown.GetItemData(selectIndex);
	CString sdata1;
	sdata1.Format("附件数据:%d",text);
	AfxMessageBox(sdata1);