DteailDlg.cpp
上传用户:cuijiu615
上传日期:2007-03-28
资源大小:45k
文件大小:8k
- // DteailDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "phonebook.h"
- #include "DteailDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // DteailDlg dialog
- DteailDlg::DteailDlg(CWnd* pParent /*=NULL*/)
- : CDialog(DteailDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(DteailDlg)
- m_age = 0;
- m_email = _T("");
- m_introduction = _T("");
- m_position = _T("");
- m_mobile = _T("");
- m_name = _T("");
- m_telphone = _T("");
- m_address = _T("");
- m_relation = _T("");
- m_sex = _T("");
- m_company = _T("");
- m_code = _T("");
- //}}AFX_DATA_INIT
- isAppend=FALSE;
- isEdit=FALSE;
- }
- void DteailDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(DteailDlg)
- DDX_Control(pDX, IDC_EDIT_CODE, m_editcode);
- DDX_Control(pDX, IDC_MOBYLE, m_editmobile);
- DDX_Control(pDX, IDC_JOB, m_editposition);
- DDX_Control(pDX, IDC_WORKPLACE, m_editcompany);
- DDX_Control(pDX, IDC_SEX, m_editsex);
- DDX_Control(pDX, IDC_RELATION, m_editrelation);
- DDX_Control(pDX, IDC_PHONE, m_edittelphone);
- DDX_Control(pDX, IDC_PLACE, m_editaddress);
- DDX_Control(pDX, IDC_NAME, m_editname);
- DDX_Control(pDX, IDC_INTRODUCTION, m_editintroduction);
- DDX_Control(pDX, IDC_EMAIL, m_editemail);
- DDX_Control(pDX, IDC_AGE, m_editage);
- DDX_Text(pDX, IDC_AGE, m_age);
- DDV_MinMaxInt(pDX, m_age, 0, 100);
- DDX_Text(pDX, IDC_EMAIL, m_email);
- DDX_Text(pDX, IDC_INTRODUCTION, m_introduction);
- DDX_Text(pDX, IDC_JOB, m_position);
- DDX_Text(pDX, IDC_MOBYLE, m_mobile);
- DDX_Text(pDX, IDC_NAME, m_name);
- DDX_Text(pDX, IDC_PHONE, m_telphone);
- DDX_Text(pDX, IDC_PLACE, m_address);
- DDX_Text(pDX, IDC_RELATION, m_relation);
- DDX_Text(pDX, IDC_SEX, m_sex);
- DDX_Text(pDX, IDC_WORKPLACE, m_company);
- DDX_Text(pDX, IDC_EDIT_CODE, m_code);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(DteailDlg, CDialog)
- //{{AFX_MSG_MAP(DteailDlg)
- ON_WM_CTLCOLOR()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // DteailDlg message handlers
- HBRUSH DteailDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = (HBRUSH)m_brush;
- if(nCtlColor==CTLCOLOR_STATIC)
- { pDC->SetBkMode(TRANSPARENT);
- return hbr;
- }
- if(nCtlColor==CTLCOLOR_EDIT)
- {
- return NULL;
- }
- return hbr;
- }
- BOOL DteailDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- m_brush.CreateSolidBrush(RGB(0,170,170));
- try
- {
- CString sDSN="ODBC;DRIVER=Microsoft Access Driver (*.mdb);DSN='';DBQ=telbook.mdb";
- m_database=new CDatabase;
- if(!m_database->Open(NULL,FALSE,FALSE,sDSN,FALSE))
- {
- AfxMessageBox("ODBC Connection Faile!");
- CDialog::EndDialog(0);
- GetParent()->PostMessage(WM_CLOSE);
- return FALSE;
- }
-
- CString strSQL="select * from personlist";
- m_pSet=new CMyRecordset(m_database);
- m_pSet->Open(CRecordset::dynaset,strSQL);
- }
- catch(CDBException* pEx)
- {
- pEx->ReportError();
- }
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void DteailDlg::ShowDetail(int nIndex)
- {
- switch(nIndex)
- {
- case -2://On first
- m_pSet->MoveFirst();
- break;
- case -1://On prevew
- m_pSet->MovePrev();
- break;
- case 1://On next
- m_pSet->MoveNext();
- break;
- case 2://On Last
- m_pSet->MoveLast();
- break;
- }
- GetValue();
- }
- int DteailDlg::GetNUM()
- {
- int i=0;
- m_pSet->MoveFirst();
- while(!m_pSet->IsEOF())
- {
- i++;
- m_pSet->MoveNext();
- }
- m_pSet->MoveFirst();
- return i;
- }
- BOOL DteailDlg::DestroyWindow()
- {
-
- if(m_pSet!=NULL)
- {
- m_pSet->Close();
- m_database->Close();
- delete m_pSet;
- delete m_database;
- }
- return CDialog::DestroyWindow();
- }
- void DteailDlg::ClearEditText()
- {
- m_address = _T("");
- m_company = _T("");
- m_email = _T("");
- m_introduction = _T("");
- m_mobile = _T("");
- m_name = _T("");
- m_position = _T("");
- m_relation = _T("");
- m_sex = _T("");
- m_telphone = _T("");
- m_age = 0;
- UpdateData(FALSE);
- }
- void DteailDlg::GetValue()
- {
- m_name = m_pSet->m_name;
- m_age = m_pSet->m_age;
- m_sex = m_pSet->m_sex;
- m_email = m_pSet->m_email;
- m_code = m_pSet->m_pager;
- m_relation = m_pSet->m_friendship;
- m_company = m_pSet->m_company;
- m_address = m_pSet->m_address;
- m_telphone = m_pSet->m_telphone;
- m_mobile = m_pSet->m_mobile;
- m_position = m_pSet->m_position;
- m_introduction = m_pSet->m_introduction;
- UpdateData(FALSE);
- }
- void DteailDlg::SetValue()
- {
- UpdateData(TRUE);
- m_pSet->m_name = m_name;
- m_pSet->m_age = m_age;
- m_pSet->m_sex = m_sex;
- m_pSet->m_email = m_email;
- m_pSet->m_pager = m_code;
- m_pSet->m_friendship= m_relation;
- m_pSet->m_company = m_company;
- m_pSet->m_address = m_address;
- m_pSet->m_telphone = m_telphone;
- m_pSet->m_mobile = m_mobile;
- m_pSet->m_position = m_position;
- m_pSet->m_introduction = m_introduction;
- }
- bool DteailDlg::IsFirstRecord()
- {
- if(m_pSet->IsBOF()&&m_pSet->IsEOF())
- {
- return TRUE;
- }
- else if(m_pSet->IsBOF())
- {
- m_pSet->MoveFirst();
- return TRUE;
- }
- else
- return FALSE;
- }
- bool DteailDlg::IsLastRecord()
- {
- if(m_pSet->IsBOF()&&m_pSet->IsEOF())
- {
- return TRUE;
- }
- else if(m_pSet->IsEOF())
- {
- m_pSet->MoveLast();
- return TRUE;
- }
- else
- return FALSE;
- }
- void DteailDlg::SetEditReadOnly(BOOL isReadOnly)
- {
- m_editname.SetReadOnlyState(isReadOnly);
- m_editage.SetReadOnlyState(isReadOnly);
- m_editsex.SetReadOnlyState(isReadOnly);
- m_editrelation.SetReadOnlyState(isReadOnly);
- m_editposition.SetReadOnlyState(isReadOnly);
- m_editemail.SetReadOnlyState(isReadOnly);
- m_edittelphone.SetReadOnlyState(isReadOnly);
- m_editmobile.SetReadOnlyState(isReadOnly);
- m_editaddress.SetReadOnlyState(isReadOnly);
- m_editcompany.SetReadOnlyState(isReadOnly);
- m_editintroduction.SetReadOnlyState(isReadOnly);
- }
- void DteailDlg::NewRecord()
- {
- try
- {
- if(!UpdateData(TRUE))
- return;
- if(m_name.IsEmpty())
- {
- MessageBox("[姓名] 字段必须填写内容!","注意",MB_ICONINFORMATION|MB_OK);
- return;
- }
- m_pSet->AddNew();
- SetValue();
- m_pSet->Update();
- m_pSet->Requery();
- m_pSet->MoveLast();
- SetEditReadOnly(TRUE);
- isAppend=FALSE;
- isEdit=FALSE;
- }
- catch(CDBException* pEx)
- {
- pEx->ReportError();
- }
- }
- void DteailDlg::DelRecord()
- {
- if(isAppend||isEdit)
- {
- m_pSet->CancelUpdate();
- SetEditReadOnly(TRUE);
- isEdit=FALSE;
- isAppend=FALSE;
- }
- else
- if(MessageBox("确定要删除这条记录吗?","删除记录",MB_YESNO|MB_ICONINFORMATION) == IDYES)
- m_pSet->Delete();
- }
- void DteailDlg::EditRecord(bool used)
- {
- if(!used)
- {
- SetEditReadOnly(FALSE);
- m_editname.SetFocus();
- }
- else
- {
- m_pSet->Edit();
- SetValue();
- m_pSet->Update();
- m_pSet->Requery();
- SetEditReadOnly(TRUE);
- UpdateData(TRUE);
- }
- }
- UINT DteailDlg::Find(CString str,int ID,bool &re)
- {
- /*ID
- 1.姓名2.联系电话3.手机号4.E-mail
- 5.邮政编码6.地址
- */
- TRACE("n%d",ID);
- UINT r=0;
- re=0;//显示是否有多个查找结果
- m_pSet->MoveFirst();
- CRecordsetStatus temp;
- m_pSet->GetStatus(temp);
- switch(ID)
- {
- case 1:
- while(!m_pSet->IsEOF())
- {
- r++;
- if(str==m_pSet->m_name)
- return r;
- m_pSet->MoveNext();
- }
- break;
- case 2:
- while(!m_pSet->IsEOF())
- {
- r++;
- if(str==m_pSet->m_telphone)
- return r;
- m_pSet->MoveNext();
- }
- break;
- case 3:
- while(!m_pSet->IsEOF())
- {
- r++;
- if(str==m_pSet->m_mobile)
- return r;
- m_pSet->MoveNext();
- }
- break;
- case 4:
- while(!m_pSet->IsEOF())
- {
- r++;
- if(str==m_pSet->m_email)
- return r;
- m_pSet->MoveNext();
- }
- break;
- case 5:
- while(!m_pSet->IsEOF())
- {
- r++;
- if(str==m_pSet->m_pager)
- return r;
- m_pSet->MoveNext();
- }
- break;
- case 6:
- while(!m_pSet->IsEOF())
- {
- r++;
- if(str==m_pSet->m_address)
- return r;
- m_pSet->MoveNext();
- }
- break;
- }
- return 0;
- }