首先编辑如下对话框应用程序:
然后为各个编辑框关联变量,为第一个编辑框关联一个String类型的变量,为第二个编辑框关联一个double类型的变量,为列表框关联一个CListBox变量,然后为列表框添加LBN_SELCHANGE消息的响应函数,然后再为添加按钮,删除按钮,添加消息响应函数。
实现代码如下:
void CTest1_7Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
int index;
int test;
UpdateData();
test=m_ListBox.FindStringExact(-1,m_name);
if(test!=LB_ERR)return ;//如果有返回
index=m_ListBox.AddString(m_name);
index=m_ListBox.SetItemData(index,m_id);
UpdateData(false);
GetDlgItem(IDC_BUTTON2)->EnableWindow(true);
}
void CTest1_7Dlg::OnButton2()
{
// TODO: Add your control notification handler code here
int index;
index=m_ListBox.GetCurSel();
m_ListBox.DeleteString(index);
index=m_ListBox.GetCurSel();
if(index=LB_ERR)
GetDlgItem(IDC_BUTTON2)->EnableWindow(false);
}
void CTest1_7Dlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
int index=m_ListBox.GetCurSel();
m_ListBox.GetText(index,m_name);
m_id=m_ListBox.GetItemData(index);
UpdateData(false);
}
本文介绍如何在VC++环境下操作列表框,包括添加、删除和选择学生信息。通过添加编辑框变量、关联列表框及按钮消息响应函数,实现了对学生信息的动态管理。示例代码展示了添加新学生信息到列表框以及删除所选信息的功能。
2483

被折叠的 条评论
为什么被折叠?



