📄 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;
}
/*对话框刚显示时修改和删除按钮处于不可点按状态*/
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);
/*将记录加入表之前,首先检查输入合法性*/
/*员工号和员工姓名均不能为空*/
/*而且员工号不能有重复*/
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))
{
//表中已经存在m_strEmployeeID的员工号
MessageBox("此员工号已经存在!","提示",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()
/*当tbEmployee表中的数据发生改变时,包括增加、删除、修改*/
/*将用于显示表中数据的ListBox中的数据进行更新操作*/
{
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表中数据
//同时,应删除这个员工的出勤记录,也就是需要删除
//这个tbCheckIn表中这个员工的数据
rsEmployee.Delete();
//删除这个员工所有考勤记录
rsCheck.DeleteEmpCheck(SelEmpId);
rsEmployee.Close();
/*删除了某个员工后,应该将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
/*将选择的员工进行修改,修改后的员工号和员工名就是目前两个编辑*/
/*框中的数据,修改的是tbEmployee表*/
/*同时,要修改和这个员工相关的出勤记录*/
/*修改成功后,要更新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
/*将当前选中的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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -