// UserInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "UserInfoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUserInfoDlg dialog


CUserInfoDlg::CUserInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUserInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUserInfoDlg)
	//}}AFX_DATA_INIT
}


void CUserInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUserInfoDlg)
	DDX_Control(pDX, IDC_EDIT_UserNote, m_edit_UserNote);
	DDX_Control(pDX, IDC_EDIT_UserName, m_edit_UserName);
	DDX_Control(pDX, IDC_EDIT_UserID, m_edit_UserId);
	DDX_Control(pDX, IDC_btn_cancel, m_btn_cancel);
	DDX_Control(pDX, IDC_Btn_OK, m_btn_ok);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CUserInfoDlg, CDialog)
	//{{AFX_MSG_MAP(CUserInfoDlg)
	ON_BN_CLICKED(IDC_Btn_OK, OnBtnOK)
	ON_BN_CLICKED(IDC_btn_cancel, Onbtncancel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUserInfoDlg message handlers

extern char *gszDbFile;
BOOL CUserInfoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_btn_ok.SetIcon(IDI_ICON_OK);
	m_btn_ok.DrawTransparent(TRUE);

	m_btn_cancel.SetIcon(IDI_ICON_CANCEL);
	m_btn_cancel.DrawTransparent(TRUE);

	//连接数据库
	CADODatabase m_pDb;
	CString strConnection;
	strConnection.Format("Driver={Microsoft Access Driver (*.mdb)};"
		"Dbq=%s;Uid=%s;Pwd=%s",gszDbFile,"","");
	if(!m_pDb.Open(strConnection))
	{
		CDialog::EndDialog(0);
		return FALSE;
	}

	CString strSql;
	CString strUserId;
	strSql.Format("select * from UserInfo");
	CADORecordset m_pRs = CADORecordset(&m_pDb);
	if(m_pRs.Open((LPCTSTR)strSql,CADORecordset::openQuery))	
	{
		while(!m_pRs.IsEof())
		{
			m_pRs.GetFieldValue("UserID",strUserId);
			m_pRs.MoveNext();
		}
		m_pRs.Close();
	}
	m_pDb.Close();

	long iUserId = atol(strUserId);
	m_strUserId.Format("%d",iUserId+1);
	m_edit_UserId.SetWindowText(m_strUserId);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CUserInfoDlg::OnBtnOK() 
{
	// TODO: Add your control notification handler code here
	m_edit_UserId.GetWindowText(m_strUserId);
	m_edit_UserName.GetWindowText(m_strUserName);
	m_edit_UserNote.GetWindowText(m_strUserNote);
	int iLength = m_strUserId.GetLength();
	if (iLength<=0)
	{
		AfxMessageBox("请输入ID号");
		return;
	}
	char cId;
	for (int i=0;i<iLength;i++)
	{
		cId = m_strUserId.GetAt(i);
		if (cId>'9' || cId<'0')
		{
			AfxMessageBox("错误：ID号只能由数字组成");
			return;
		}
	}
	CDialog::OnOK();
}

void CUserInfoDlg::Onbtncancel() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}
