1 //添加按钮点击事件 2 void CMFCApplication4Dlg::OnBnClickedButton1() 3 { 4 CString strText; 5 //获取文本框的值 6 GetDlgItemText(IDC_EDIT1, strText); 7 //添加到组合框中 8 m_cmb_demo.AddString(strText); 9 m_cmb_demo.SetCurSel(m_cmb_demo.GetCount() - 1);//设置当前选中 10 //添加到列表框中 11 m_list_demo.AddString(strText); 12 m_list_demo.SetCurSel(m_list_demo.GetCount() - 1);//设置当前选中 13 } 14 15 //删除按钮点击事件 16 void CMFCApplication4Dlg::OnBnClickedButton2() 17 { 18 //删除当前选中 19 int nIndex; 20 nIndex = m_cmb_demo.GetCurSel(); 21 if (nIndex > -1) { 22 m_cmb_demo.DeleteString(nIndex); 23 if (nIndex < m_cmb_demo.GetCount()) 24 m_cmb_demo.SetCurSel(nIndex); 25 else 26 m_cmb_demo.SetCurSel(0); 27 } 28 29 nIndex = m_list_demo.GetCurSel(); 30 if (nIndex > -1) { 31 m_list_demo.DeleteString(nIndex); 32 if (nIndex < m_list_demo.GetCount()) 33 m_list_demo.SetCurSel(nIndex); 34 else 35 m_list_demo.SetCurSel(0); 36 } 37 }