组合框,列表框

组合框封装类: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);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值