?? 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("簽到用戶");
/*對話框剛顯示時修改和刪除按鈕處于不可點按狀態(tài)*/
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()
/*當(dāng)tbUser表中的數(shù)據(jù)發(fā)生改變時,包括增加、刪除、修改*/
/*將用于顯示表中數(shù)據(jù)的ListBox中的數(shù)據(jù)進行更新操作*/
{
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," 用戶名 用戶密碼 權(quán)限級別");
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);
/*將記錄加入表之前,首先檢查輸入合法性*/
/*用戶名和權(quán)限級別不能為空*/
/*而且用戶名不能有重復(fù)*/
if (this->m_strUserName == "")
{
MessageBox("必須輸入用戶名!","提示",MB_ICONINFORMATION|MB_OK);
pEditName->SetFocus();
return;
}
if (this->m_AuthorityCombo.GetCurSel() == CB_ERR )
{
MessageBox("必須選擇用戶權(quán)限級別!","提示",MB_ICONINFORMATION|MB_OK);
pEditName->SetFocus();
return;
}
CUserRecordset rsUser;
if (rsUser.IsRepeatUser(this->m_strUserName))
{
//表中已經(jīng)存在m_StrUserName的用戶
MessageBox("此用戶已經(jīng)存在!","提示",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
/*將刪除按鈕設(shè)為可以點按*/
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();
/*刪除了某個用戶后,應(yīng)該將ListBox中此條記錄刪除*/
/*并將兩個Edit清空*/
this->m_ListBox.DeleteString(iSel);
}
else
{
MessageBox("找不到符合條件的用戶!","提示",MB_ICONINFORMATION|MB_OK);
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -