📄 dlguser.cpp
字号:
// dlgUser.cpp : implementation file
//
#include "stdafx.h"
#include "CheckIn.h"
#include "dlgUser.h"
#include "UserRecordset.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CdlgUser dialog
CdlgUser::CdlgUser(CWnd* pParent /*=NULL*/)
: CDialog(CdlgUser::IDD, pParent)
{
//{{AFX_DATA_INIT(CdlgUser)
m_strUserName = _T("");
m_strPass1 = _T("");
//}}AFX_DATA_INIT
}
void CdlgUser::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CdlgUser)
DDX_Control(pDX, IDC_COMBO_AUTHORITY, m_AuthorityCombo);
DDX_Control(pDX, IDC_LIST_USER, m_ListBox);
DDX_Text(pDX, IDC_EDIT_USERNAME, m_strUserName);
DDX_Text(pDX, IDC_EDIT_PASSWORD1, m_strPass1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CdlgUser, CDialog)
//{{AFX_MSG_MAP(CdlgUser)
ON_BN_CLICKED(IDC_OK, OnMyClose)
ON_LBN_SELCANCEL(IDC_LIST_USER, OnSelcancelListUser)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_LBN_SELCHANGE(IDC_LIST_USER, OnSelchangeListUser)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CdlgUser message handlers
void CdlgUser::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CdlgUser::OnMyClose()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}
BOOL CdlgUser::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
if (!UpdateUserListBox())
{
MessageBox("初始化失败!","提示",MB_ICONINFORMATION|MB_OK);
return FALSE;
}
this->m_AuthorityCombo.AddString("超级用户");
this->m_AuthorityCombo.AddString("签到用户");
/*对话框刚显示时修改和删除按钮处于不可点按状态*/
CButton *pDelBtn = (CButton*)this->GetDlgItem(IDC_BUTTON_DELETE);
pDelBtn->EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CdlgUser::UpdateUserListBox()
/*当tbUser表中的数据发生改变时,包括增加、删除、修改*/
/*将用于显示表中数据的ListBox中的数据进行更新操作*/
{
this->m_ListBox.ResetContent();
CUserRecordset rsUser;
try
{
if(!rsUser.IsOpen())
rsUser.Open();
}
catch(CDBException *e)
{
AfxMessageBox(e->m_strError);
return false;
}
long rsCount = rsUser.MyGetRecordCount();
if(rsCount > 0)//表中有记录,并且把表中记录加入到对话框的ListBox中
{
this->m_ListBox.InsertString(0," 用户名 用户密码 权限级别");
if (!rsUser.IsBOF ())
rsUser.MoveFirst();
int i = 1;
while(!rsUser.IsEOF())
{
this->m_ListBox.InsertString(i,rsUser.m_UserName+" "+rsUser.m_Password+" "+rsUser.m_Authority);
rsUser.MoveNext();
i++;
}
rsUser.Close();
}
return true;
}
void CdlgUser::OnSelcancelListUser()
{
// TODO: Add your control notification handler code here
}
void CdlgUser::OnButtonAdd()
{
// TODO: Add your control notification handler code here
CEdit *pEditName = (CEdit*)this->GetDlgItem(IDC_EDIT_USERNAME);
CEdit *pEditPass = (CEdit*)this->GetDlgItem(IDC_EDIT_PASSWORD1);
UpdateData(true);
/*将记录加入表之前,首先检查输入合法性*/
/*用户名和权限级别不能为空*/
/*而且用户名不能有重复*/
if (this->m_strUserName == "")
{
MessageBox("必须输入用户名!","提示",MB_ICONINFORMATION|MB_OK);
pEditName->SetFocus();
return;
}
if (this->m_AuthorityCombo.GetCurSel() == CB_ERR )
{
MessageBox("必须选择用户权限级别!","提示",MB_ICONINFORMATION|MB_OK);
pEditName->SetFocus();
return;
}
CUserRecordset rsUser;
if (rsUser.IsRepeatUser(this->m_strUserName))
{
//表中已经存在m_StrUserName的用户
MessageBox("此用户已经存在!","提示",MB_ICONINFORMATION|MB_OK);
}
else//表中不存在m_StrUserName的用户,可以增加
{
CString strAuthority;
this->m_AuthorityCombo.GetWindowText(strAuthority);
if (rsUser.AddUser(this->m_strUserName,this->m_strPass1,strAuthority))
{
MessageBox("增加成功!","提示",MB_ICONINFORMATION|MB_OK);
this->UpdateUserListBox();
}
else
MessageBox("增加失败!","提示",MB_ICONINFORMATION|MB_OK);
}
pEditName->SetWindowText("");
pEditPass->SetWindowText("");
pEditName->SetFocus();
}
void CdlgUser::OnSelchangeListUser()
{
// TODO: Add your control notification handler code here
/*将删除按钮设为可以点按*/
int iSel = this->m_ListBox.GetCurSel();
if (iSel!=0)
{
CButton *pDelBtn = (CButton*)this->GetDlgItem(IDC_BUTTON_DELETE);
pDelBtn->EnableWindow(true);
}
}
void CdlgUser::OnButtonDelete()
{
// 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//真的要删除
{
CUserRecordset rsUser;
/*首先从ListBox的选择项中分离出用户名*/
CString strTmp;
this->m_ListBox.GetText(iSel,strTmp);
CString SelUserName;
CString tmp;
for(int i=0;;i++)
{
tmp = strTmp.Mid(i,1);
if (tmp == " ")
break;
}
SelUserName = strTmp.Mid(0,i);
/*定位记录*/
BOOL bFind = FALSE;
if (!rsUser.IsOpen())
rsUser.Open();
if(!rsUser.IsBOF())
rsUser.MoveFirst();
while(!rsUser.IsEOF())
{
if (rsUser.m_UserName== SelUserName )
{
bFind = TRUE;
break;
}
rsUser.MoveNext();
}
if (bFind)
{
rsUser.Delete();
rsUser.Close();
/*删除了某个用户后,应该将ListBox中此条记录删除*/
/*并将两个Edit清空*/
this->m_ListBox.DeleteString(iSel);
}
else
{
MessageBox("找不到符合条件的用户!","提示",MB_ICONINFORMATION|MB_OK);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -