• MFC信息管理系统


    头文件声明

        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判断是否修改或已保存。
    
    
  • 相关阅读:
    归并排序
    边割集
    NBUT 1225 NEW RDSP MODE I 2010辽宁省赛
    NBUT 1218 You are my brother 2010辽宁省赛
    NBUT 1220 SPY 2010辽宁省赛
    NBUT 1219 Time 2010辽宁省赛
    NBUT 1223 Friends number 2010辽宁省赛
    NBUT 1217 Dinner 2010辽宁省赛
    第七届蓝桥杯个人赛省赛--C语言B组
    2017广东工业大学程序设计竞赛决赛 tmk买礼物
  • 原文地址:https://www.cnblogs.com/horizonhz/p/13475418.html
Copyright © 2020-2023  润新知