?? dlgemployee.cpp
字號:
// dlgEmployee.cpp : implementation file
//
#include "stdafx.h"
#include "CheckIn.h"
#include "dlgEmployee.h"
#include "EmployeeRecordset.h"
#include "CheckRecordset.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CdlgEmployee dialog
CdlgEmployee::CdlgEmployee(CWnd* pParent /*=NULL*/)
: CDialog(CdlgEmployee::IDD, pParent)
{
//{{AFX_DATA_INIT(CdlgEmployee)
m_strEmployeeID = _T("");
m_strEmployeeName = _T("");
//}}AFX_DATA_INIT
}
void CdlgEmployee::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CdlgEmployee)
DDX_Control(pDX, IDC_LIST_EMPLOYEE, m_ListBox);
DDX_Text(pDX, IDC_EDIT_ID, m_strEmployeeID);
DDX_Text(pDX, IDC_EDIT_NAME, m_strEmployeeName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CdlgEmployee, CDialog)
//{{AFX_MSG_MAP(CdlgEmployee)
ON_BN_CLICKED(IDC_OK, OnCloseDlg)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_LBN_SELCHANGE(IDC_LIST_EMPLOYEE, OnSelchangeListEmployee)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CdlgEmployee message handlers
void CdlgEmployee::OnCloseDlg()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
void CdlgEmployee::OnOK()
{
// TODO: Add your control notification handler code here
//CDialog::OnOK();
}
BOOL CdlgEmployee::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
/*把tbEmployee表中的所有員工顯示在對話框的ListBox中*/
//CListBox *pListBox = (CListBox*)this->GetDlgItem(IDC_LIST_EMPLOYEE);
if (!UpdateEmpListBox())
{
MessageBox("初始化失敗!","提示",MB_ICONINFORMATION|MB_OK);
return FALSE;
}
/*對話框剛顯示時修改和刪除按鈕處于不可點按狀態(tài)*/
CButton *pDelBtn = (CButton*)this->GetDlgItem(IDC_DELETE);
CButton *pModifyBtn = (CButton*)this->GetDlgItem(IDC_MODIFY);
pDelBtn->EnableWindow(false);
pModifyBtn->EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CdlgEmployee::OnAdd()
{
// TODO: Add your control notification handler code here
CEdit *pEditID = (CEdit*)this->GetDlgItem(IDC_EDIT_ID);
CEdit *pEditName = (CEdit*)this->GetDlgItem(IDC_EDIT_NAME);
UpdateData(true);
/*將記錄加入表之前,首先檢查輸入合法性*/
/*員工號和員工姓名均不能為空*/
/*而且員工號不能有重復(fù)*/
if (m_strEmployeeID == "")
{
MessageBox("必須輸入員工號!","提示",MB_ICONINFORMATION|MB_OK);
pEditID->SetFocus();
return;
}
if (m_strEmployeeName == "")
{
MessageBox("必須輸入員工姓名!","提示",MB_ICONINFORMATION|MB_OK);
pEditName->SetFocus();
return;
}
CEmployeeRecordset rsEmployee;
if (rsEmployee.IsRepeatEmployeeID(this->m_strEmployeeID))
{
//表中已經(jīng)存在m_strEmployeeID的員工號
MessageBox("此員工號已經(jīng)存在!","提示",MB_ICONINFORMATION|MB_OK);
}
else//表中不存在m_strEmployeeID的員工號,可以增加
{
if (rsEmployee.AddEmployee(this->m_strEmployeeID,this->m_strEmployeeName))
{
MessageBox("增加成功!","提示",MB_ICONINFORMATION|MB_OK);
this->UpdateEmpListBox();
}
else
MessageBox("增加失敗!","提示",MB_ICONINFORMATION|MB_OK);
}
pEditID->SetWindowText("");
pEditName->SetWindowText("");
pEditID->SetFocus();
}
BOOL CdlgEmployee::UpdateEmpListBox()
/*當(dāng)tbEmployee表中的數(shù)據(jù)發(fā)生改變時,包括增加、刪除、修改*/
/*將用于顯示表中數(shù)據(jù)的ListBox中的數(shù)據(jù)進(jìn)行更新操作*/
{
this->m_ListBox.ResetContent();
CEmployeeRecordset rsEmployee;
try
{
if(!rsEmployee.IsOpen())
rsEmployee.Open();
}
catch(CDBException *e)
{
AfxMessageBox(e->m_strError);
return false;
}
long rsCount = rsEmployee.MyGetRecordCount();
if(rsCount > 0)//表中有記錄,并且把表中記錄加入到對話框的ListBox中
{
this->m_ListBox.InsertString(0," 員工號 員工姓名");
if (!rsEmployee.IsBOF ())
rsEmployee.MoveFirst();
int i = 1;
while(!rsEmployee.IsEOF())
{
this->m_ListBox.InsertString(i,rsEmployee.m_EmployeeID+" "+rsEmployee.m_EmployeeName);
rsEmployee.MoveNext();
i++;
}
rsEmployee.Close();
}
//this->UpdateData(false);
return true;
}
void CdlgEmployee::OnDelete()
{
// TODO: Add your control notification handler code here
/*首先要判斷是否選擇了某個員工*/
int iSel = this->m_ListBox.GetCurSel();
if (iSel == LB_ERR)
{
MessageBox("請選擇要刪除的員工!","提示",MB_ICONINFORMATION|MB_OK);
}
else//選擇了某個員工
{
if (MessageBox("真的要刪除這個員工嗎嗎?(Y/N)","提示",MB_YESNO)==IDNO)
return;
else//真的要刪除
{
CEmployeeRecordset rsEmployee;
CCheckRecordset rsCheck;
/*首先從ListBox的選擇項中分離出員工號*/
CString strTmp;
this->m_ListBox.GetText(iSel,strTmp);
CString SelEmpId;
CString tmp;
for(int i=0;;i++)
{
tmp = strTmp.Mid(i,1);
if (tmp == " ")
break;
}
SelEmpId = strTmp.Mid(0,i);
/*定位記錄*/
BOOL bFind = FALSE;
if (!rsEmployee.IsOpen())
rsEmployee.Open();
if(!rsEmployee.IsBOF())
rsEmployee.MoveFirst();
while(!rsEmployee.IsEOF())
{
if (rsEmployee.m_EmployeeID == SelEmpId )
{
bFind = TRUE;
break;
}
rsEmployee.MoveNext();
}
if (bFind)
{
//找到選擇的員工記錄,刪除tbEmployee表中數(shù)據(jù)
//同時,應(yīng)刪除這個員工的出勤記錄,也就是需要刪除
//這個tbCheckIn表中這個員工的數(shù)據(jù)
rsEmployee.Delete();
//刪除這個員工所有考勤記錄
rsCheck.DeleteEmpCheck(SelEmpId);
rsEmployee.Close();
/*刪除了某個員工后,應(yīng)該將ListBox中此條記錄刪除*/
/*并將兩個Edit清空*/
this->m_ListBox.DeleteString(iSel);
this->m_strEmployeeID = "";
this->m_strEmployeeName = "";
this->UpdateData(false);
}
else
{
MessageBox("找不到符合條件的員工!","提示",MB_ICONINFORMATION|MB_OK);
}
}
}
}
void CdlgEmployee::OnModify()
{
// TODO: Add your control notification handler code here
/*將選擇的員工進(jìn)行修改,修改后的員工號和員工名就是目前兩個編輯*/
/*框中的數(shù)據(jù),修改的是tbEmployee表*/
/*同時,要修改和這個員工相關(guān)的出勤記錄*/
/*修改成功后,要更新ListBox*/
CEmployeeRecordset rsEmployee;
CCheckRecordset rsCheck;
CEdit *pEmpIdEdit = (CEdit*)this->GetDlgItem(IDC_EDIT_ID);
CEdit *pEmpNameEdit = (CEdit*)this->GetDlgItem(IDC_EDIT_NAME);
CString StrEmpId;
CString StrEmpName;
pEmpIdEdit->GetWindowText(StrEmpId);
pEmpNameEdit->GetWindowText(StrEmpName);
/*首先從ListBox的選擇項中分離出員工號*/
CString strTmp;
int iSel = this->m_ListBox.GetCurSel();
this->m_ListBox.GetText(iSel,strTmp);
CString SelEmpId;
CString tmp;
for(int i=0;;i++)
{
tmp = strTmp.Mid(i,1);
if (tmp == " ")
break;
}
SelEmpId = strTmp.Mid(0,i);
BOOL bFind = FALSE;
if (!rsEmployee.IsOpen())
rsEmployee.Open();
if(!rsEmployee.IsBOF())
rsEmployee.MoveFirst();
while(!rsEmployee.IsEOF())
{
if (rsEmployee.m_EmployeeID == SelEmpId)
{
bFind = TRUE;
break;
}
rsEmployee.MoveNext();
}
if(bFind)//定位在這個記錄上
{
//修改tbEmployee表
rsEmployee.Edit();
rsEmployee.m_EmployeeID = StrEmpId;
rsEmployee.m_EmployeeName = StrEmpName;
rsEmployee.Update();
rsEmployee.Close();
//更新ListBox
this->m_ListBox.DeleteString(iSel);
this->m_ListBox.InsertString(iSel,StrEmpId+" "+StrEmpName);
//this->UpdateEmpListBox();
if (StrEmpId != SelEmpId)
{
//修改這個員工在出勤表tbCheckIn中的員工號
//將SelEmpId的員工號修改為StrEmpId
rsCheck.ChageEmpId(SelEmpId,StrEmpId);
}
}
else
{
MessageBox("這個員工不在員工表中!","提示",MB_ICONINFORMATION|MB_OK);
}
}
void CdlgEmployee::OnSelchangeListEmployee()
{
// TODO: Add your control notification handler code here
/*將當(dāng)前選中的ListBox中的員工姓名和員工號分別填入Edit中*/
int iSel = this->m_ListBox.GetCurSel();
if (iSel!=0)//選擇的是第一項
{
CButton *pDelBtn = (CButton*)this->GetDlgItem(IDC_DELETE);
CButton *pModifyBtn = (CButton*)this->GetDlgItem(IDC_MODIFY);
pDelBtn->EnableWindow(true);
pModifyBtn->EnableWindow(true);
/*首先從ListBox的選擇項中分離出員工號*/
CString strTmp;
this->m_ListBox.GetText(iSel,strTmp);
CString SelEmpId;
CString SelEmpName;
CString tmp;
for(int i=0;;i++)
{
tmp = strTmp.Mid(i,1);
if (tmp == " ")
break;
}
SelEmpId = strTmp.Mid(0,i);
/*從ListBox中的選擇項中分離處員工姓名*/
for(i=strTmp.GetLength();;i--)
{
tmp = strTmp.Mid(i,1);
if (tmp == " ")
break;
}
SelEmpName = strTmp.Mid(i+1,strTmp.GetLength()-i);
this->m_strEmployeeID = SelEmpId;
this->m_strEmployeeName = SelEmpName;
this->UpdateData(false);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -