组合框封装类:CComboBox
列表框封装类CListBox
首先将列表框和组合框都绑定控件变量,我们希望当按下添加按钮时,将数据从编辑框分别添加到列表框和组合框中去
添加按钮代码:
void Cdemo7Dlg::OnBnClickedButAdd()
{
// TODO: 在此添加控件通知处理程序代码
CString strText;
GetDlgItemText(IDC_EDT_TEXT,strText);
m_cmb_demo.AddString(strText);//添加到组合框里
m_cmb_demo.SetCurSel(m_cmb_demo.GetCount()-1);//当前选中新添加的成员
m_lst_demo.AddString(strText);
m_lst_demo.SetCurSel(m_lst_demo.GetCount()-1);
}
在添加的时候,列表框会自动排序内容,将其sort属性设置为false即可取消。
删除按钮代码:
void Cdemo7Dlg::OnBnClickedButDelete()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex; //索引
nIndex=m_cmb_demo.GetCurSel();//当前选中的
if(nIndex>-1) //有选中的
{
m_cmb_demo.DeleteString(nIndex); //删除选中
if(nIndex<m_cmb_demo.GetCount()) //如果nIndex索引还在范围内,则选中当前索引,否则选中第一个
m_cmb_demo.SetCurSel(nIndex);
else
m_cmb_demo.SetCurSel(0);
}
nIndex=m_lst_demo.GetCurSel();
if(nIndex>-1)
{
m_lst_demo.DeleteString(nIndex);
if(nIndex<m_lst_demo.GetCount())
m_lst_demo.SetCurSel(nIndex);
else
m_lst_demo.SetCurSel(0);
}
}