头文件声明
bool CheckNumber(LPCTSTR Number); void OnBnClickedLoad(); struct sInfo{ int nNum; TCHAR sName[10]; int nSala; COleDateTime tDate; }; BOOL m_bModified;
添加信息函数:
void CMFCApplication2Dlg::OnBnClickedAdd()
{
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST);
CString str;
GetDlgItemText(IDC_Num, str);
if (CheckNumber(str))
return;
int i = pList->GetItemCount();
GetDlgItemText(IDC_Num, str);
pList->InsertItem(i, str);
GetDlgItemText(IDC_Name, str);
pList->SetItemText(i, 1, str);
GetDlgItemText(IDC_Sala, str);
pList->SetItemText(i, 2, str);
GetDlgItemText(IDC_Date, str);
pList->SetItemText(i, 3, str);
m_bModified = TRUE;
}
添加时查重:
//查重,重复数据不可添加
bool CMFCApplication2Dlg::CheckNumber(LPCTSTR Number)
{
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST);
int i = 0;
int nCount = pList->GetItemCount();
while (i < nCount){
if (pList->GetItemText(i, 0) == Number){
MessageBox(_T("信息已存在!"));
SetFocus();
return true;
}
i++;
}
return false;
}
删除:
void CMFCApplication2Dlg::OnBnClickedDel()
{
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST);
int nSel = pList->GetSelectionMark();
if (!nSel){
MessageBox(_T("请选中删除数据!"));
return;
}
else{
CString str = pList->GetItemText(nSel, 0);
if (MessageBox(_T("确定删除") + str + _T("吗?"), _T("确认删除"), MB_YESNO) == IDYES)
pList->DeleteItem(nSel);
}
m_bModified = TRUE;
}
修改:
void CMFCApplication2Dlg::OnBnClickedMod()
{
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST);
if (!pList->GetSelectedCount())
{
MessageBox(_T("无选中行!"));
return;
}
POSITION pos = pList->GetFirstSelectedItemPosition();
int nSel = pList->GetNextSelectedItem(pos);
CString str = pList->GetItemText(nSel, 0);
if (MessageBox(_T("确定修改") + str + _T("吗?"), _T("确认修改"), MB_YESNO) == IDYES)
{
GetDlgItemText(IDC_Name, str);
pList->SetItemText(nSel, 1, str);
GetDlgItemText(IDC_Sala, str);
pList->SetItemText(nSel, 2, str);
GetDlgItemText(IDC_Date, str);
pList->SetItemText(nSel, 3, str);
}
m_bModified = TRUE;
}
加载显示:
void CMFCApplication2Dlg::OnBnClickedLoad()
{
CFile file;
file.Open(_T("./employee.dat"), CFile::modeRead);
CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST);
sInfo info;
int i = 0;
CString str;
while (file.Read(&info, sizeof(info)) == sizeof(info))
{
str.Format(_T("%d"), info.nNum);
pList->InsertItem(i, str);
pList->SetItemText(i, 1, info.sName);
str.Format(_T("%d"), info.nSala);
pList->SetItemText(i, 2, str);
str.Format(_T("%d年%d月%d日"), info.tDate.GetYear(), info.tDate.GetMonth(), info.tDate.GetDay());
pList->SetItemText(i, 3, str);
++i;
}
}
保存:
void CMFCApplication2Dlg::OnBnClickedSave() { CFile file; file.Open(_T("./employee.dat"), CFile::modeCreate | CFile::modeWrite); sInfo info; CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST); int i = 0, nCount = pList->GetItemCount(); while (i < nCount){ info.nNum = _tstoi(pList->GetItemText(i, 0)); _tcscpy(info.sName, pList->GetItemText(i, 1)); info.nSala = _tstoi(pList->GetItemText(i, 2)); info.tDate.ParseDateTime(pList->GetItemText(i, 3), VAR_DATEVALUEONLY); int size = sizeof(info); file.Write(&info, size); i++; } file.Close(); m_bModified = FALSE; } 保存实现需在 <stdafx.h>中加入 #define _CRT_SECURE_NO_WARNINGS 防止报错。
关闭时提示保存:
void CMFCApplication2Dlg::OnBnClickedCancel() { if (m_bModified == TRUE){ if (MessageBox(_T("是否保存修改过的数据?"),NULL, MB_YESNO) == IDYES){ OnBnClickedSave(); } } CDialogEx::OnCancel(); }
用 m_bModified判断是否修改或已保存。