📄 employdlg.cpp
字号:
// EmployDlg.cpp : implementation file
//
#include "stdafx.h"
#include "salarly.h"
#include "EmployDlg.h"
#include "EmploySet.h"
#include "EmployOdlg.h"
#include "DelectEmployDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEmployDlg dialog
CEmployDlg::CEmployDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEmployDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEmployDlg)
m_strAddress = _T("");
m_strDepartment = _T("");
m_strID = _T("");
m_strJob = _T("");
m_strName = _T("");
m_strQuery = _T("");
m_strSex = _T("");
m_strTel = _T("");
m_Birthday = 0;
//}}AFX_DATA_INIT
}
void CEmployDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEmployDlg)
DDX_Control(pDX, IDC_LIST1, m_List1);
DDX_Text(pDX, IDC_ADDRESS, m_strAddress);
DDX_Text(pDX, IDC_DEPARTMENT, m_strDepartment);
DDX_Text(pDX, IDC_ID, m_strID);
DDX_Text(pDX, IDC_JOB, m_strJob);
DDX_Text(pDX, IDC_NAME, m_strName);
DDX_Text(pDX, IDC_QUERY, m_strQuery);
DDX_Text(pDX, IDC_SEX, m_strSex);
DDX_Text(pDX, IDC_TEL, m_strTel);
DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER1, m_Birthday);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEmployDlg, CDialog)
//{{AFX_MSG_MAP(CEmployDlg)
ON_BN_CLICKED(IDC_BUTTON_SHOW, OnButtonShow)
ON_BN_CLICKED(IDC_BUTTON_QUERY, OnButtonQuery)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
ON_BN_CLICKED(IDC_BUTTON_DELET, OnButtonDelet)
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEmployDlg message handlers
BOOL CEmployDlg::OnInitDialog() //初始化List控件
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_List1.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);//设置List网格
m_List1.InsertColumn(0,_T("员工号"),LVCFMT_IMAGE|LVCFMT_LEFT);
m_List1.InsertColumn(1,_T("姓名"));
m_List1.InsertColumn(2,_T("部门"));
m_List1.InsertColumn(3,_T("性别"));
m_List1.InsertColumn(4,_T("职务"));
m_List1.InsertColumn(5,_T("电话"));
m_List1.InsertColumn(6,_T("生日"));
m_List1.InsertColumn(7,_T("住址"));
int j;
for(j=0; j <=7; j++)
{
m_List1.SetColumnWidth(j, 80);
}
m_List1.SetColumnWidth(2, 50);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CEmployDlg::OnButtonShow()
{
// TODO: Add your control notification handler code here
m_List1.DeleteAllItems(); //清除LIST控件的当前显示
int i=0;
CEmploySet m_Set;
m_Set.Open(); //打开记录集
m_Set.MoveFirst(); //移到数据表中的第一条记录
do
{ //把数据表中的内容显示到LIST控件中
CString s;
m_List1.InsertItem(i,m_Set.m_ID,0);
m_List1.SetItemText(i,1,m_Set.m_name);
m_List1.SetItemText(i,2,m_Set.m_department);
m_List1.SetItemText(i,3,m_Set.m_sex);
m_List1.SetItemText(i,4,m_Set.m_job);
m_List1.SetItemText(i,5,m_Set.m_tel);
m_List1.SetItemText(i,6,m_Set.m_birthday.Format("%Y-%#m-%#d"));
m_List1.SetItemText(i,7,m_Set.m_address);
i++;
m_Set.MoveNext();
} while(!m_Set.IsEOF());
m_Set.Close();
}
void CEmployDlg::OnButtonQuery() //员工信息的查询功能
{
// TODO: Add your control notification handler code here
CEmploySet cSet;
UpdateData(); //更新数据
CString str;
m_strQuery.TrimLeft (); //从给定的查询编号中删除首部的空格及控制字符
if(m_strQuery.IsEmpty ()) //查询的编辑控为空时
{
MessageBox("要查询的员工号不能为空!"); //弹出系统消息对话框
return;
}
str=m_strQuery;
cSet.m_strFilter.Format("ID='%s'",str);
//指定查询条件
cSet.Open();
if(!cSet.IsEOF()) //如果打开记录集有记录
{
m_strDepartment=cSet.m_department; //使数据表中的数据 和对话框中的要求一一对应
m_strName=cSet.m_name;
m_strSex=cSet.m_sex;
m_strTel=cSet.m_tel;
m_strAddress=cSet.m_address;
m_Birthday=cSet.m_birthday;
m_strJob=cSet.m_job;
m_strID=cSet.m_ID;
UpdateData(FALSE); //自动更新表单中控件显示的内容
}
else
MessageBox("查询的员工记录不存在!"); //否则弹出系统消息对话框
if(cSet.IsOpen())cSet.Close(); //如果记录集打开则关闭
}
void CEmployDlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
CEmployOdlg dlg;
CEmploySet pSet;
if(dlg.DoModal()==IDOK){ //单击"确定",弹出添加员工信息的对话框
pSet.Open(); //打开记录集
pSet.AddNew(); //添加新记录
//使对话框中的信息和数据表中的一一对应,添加都数据表中
pSet.m_department =dlg.m_strDepartment;
pSet.m_ID =dlg.m_strID;
pSet.m_name =dlg.m_strName;
pSet.m_sex =dlg.m_strSex;
pSet.m_job =dlg.m_strJob;
pSet.m_birthday =dlg.m_Birthday;
pSet.m_tel =dlg.m_strTel;
pSet.m_address =dlg.m_strAddress;
pSet.Update(); //数据表的更新
MessageBox("添加成功!"); //弹出系统消息对话框
pSet.Requery(); //更新数据库中的数据
}
}
void CEmployDlg::OnButtonEdit() //员工信息的修改功能
{
// TODO: Add your control notification handler code here
CEmploySet pSet;
UpdateData(); //读取控件中的数据
CString str;
m_strQuery.TrimLeft (); //从给定的查询结果中删除首部的空格及控制字符
if(m_strQuery.IsEmpty ()) //判断查询的员工号的结果是否为空
{
MessageBox("要修改的员工号不能为空!");
return;
}
str=m_strQuery;
pSet.m_strFilter.Format("ID='%s'",m_strID); //格式化
pSet.Open(); //打开数据表
pSet.Edit(); //编辑数据表中的内容
//使数据表中内容和对话框中的内容一一对应
pSet.m_department=m_strDepartment;
pSet.m_ID= m_strID;
pSet.m_name=m_strName;
pSet.m_sex= m_strSex;
pSet.m_job=m_strJob;
pSet.m_birthday= m_Birthday;
pSet.m_tel= m_strTel;
pSet.m_address= m_strAddress;
pSet.Update();
UpdateData(FALSE); //把数据表中的内容显示到控件中
MessageBox("修改成功");
pSet.Requery(); //数据表更新
pSet.Close(); //关闭数据表
if(pSet.IsOpen())pSet.Close(); //关闭数据表
}
void CEmployDlg::OnButtonDelet() //员工信息的删除功能,跳转到删除对话框
{
// TODO: Add your control notification handler code here
CDelectEmployDlg dlg;
dlg.DoModal();
}
void CEmployDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -