📄 transactionrecord.cpp
字号:
// TransactionRecord.cpp : implementation file
//
#include "stdafx.h"
#include "office.h"
#include "TransactionRecord.h"
#include "transaction.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTransactionRecord dialog
extern int flag;
CTransactionRecord::CTransactionRecord(CWnd* pParent /*=NULL*/)
: CDialog(CTransactionRecord::IDD, pParent)
{
//{{AFX_DATA_INIT(CTransactionRecord)
m_content = _T("");
m_transactionType = _T("");
m_transactionDate = COleDateTime::GetCurrentTime();//GetCurrentTime()是个静态的函数;
m_transactionSubject = _T("");
//}}AFX_DATA_INIT
}
void CTransactionRecord::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTransactionRecord)
DDX_Control(pDX, IDC_LIST, m_list);
DDX_Text(pDX, IDC_CONTENT, m_content);
DDX_CBString(pDX, IDC_TRANSACTIONTYPE, m_transactionType);
DDX_DateTimeCtrl(pDX, IDC_TRANSACTIONDATE, m_transactionDate);
DDX_Text(pDX, IDC_SUBJECT, m_transactionSubject);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTransactionRecord, CDialog)
//{{AFX_MSG_MAP(CTransactionRecord)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_SAVE, OnSave)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_BN_CLICKED(IDC_CANCELLATION, OnCancellation)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTransactionRecord message handlers
//extern COfficeApp theApp;//外部定义
//#include <COMDEF.H>
BOOL CTransactionRecord::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
DWORD style;
style=m_list.GetExStyle();//读取样式;
style=(style|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT)&(~LVS_EX_CHECKBOXES);//设置样式;
m_list.SetExtendedStyle(style);
m_list.InsertColumn(0,"分类",LVCFMT_LEFT,90);
m_list.InsertColumn(1,"日期",LVCFMT_LEFT,90);
m_list.InsertColumn(2,"主题",LVCFMT_LEFT,90);
cwnd_list=GetDlgItem(IDC_LIST);//
cwnd_transactionType=GetDlgItem(IDC_TRANSACTIONTYPE);
cwnd_transactionDate=GetDlgItem(IDC_TRANSACTIONDATE);
cwnd_transactionSubject=GetDlgItem(IDC_SUBJECT);
cwnd_content=GetDlgItem(IDC_CONTENT);
cwnd_add=GetDlgItem(IDC_ADD);
cwnd_delete=GetDlgItem(IDC_DELETE);
cwnd_modify=GetDlgItem(IDC_MODIFY);
cwnd_save=GetDlgItem(IDC_SAVE);
cwnd_cancel=GetDlgItem(IDC_CANCELLATION);
::CoInitialize(NULL);
_ConnectionPtr m_pConn("ADODB.Connection");
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\DataSource\\office1.mdb;Persist Security Info=False","","",adConnectUnspecified);
_RecordsetPtr m_pRecordset("ADODB.Recordset");//记录集
//打开
m_pRecordset->Open("Select*From 事务表",_variant_t(m_pConn,true),adOpenDynamic,adLockPessimistic,adCmdText);
int i=0;
//循环将库中的数据读出(_bstr_t)来强制转换
while(!m_pRecordset->EndOfFile)
{
m_list.InsertItem(i,(_bstr_t)m_pRecordset->GetCollect("事务类型"));
m_list.SetItemText(i,1,(_bstr_t)m_pRecordset->GetCollect("事务日期"));
m_list.SetItemText(i,2,(_bstr_t)m_pRecordset->GetCollect("事务主题"));
i++;
m_pRecordset->MoveNext();
}
//设置是否可用
cwnd_list->EnableWindow(true);
cwnd_transactionType->EnableWindow(false);
((CComboBox*)cwnd_transactionType)->AddString("娱乐");
((CComboBox*)cwnd_transactionType)->AddString("购物");
((CComboBox*)cwnd_transactionType)->AddString("进餐");
((CComboBox*)cwnd_transactionType)->AddString("接待");
((CComboBox*)cwnd_transactionType)->AddString("出差");
((CComboBox*)cwnd_transactionType)->AddString("部门计划");
((CComboBox*)cwnd_transactionType)->AddString("活动");
cwnd_transactionDate->EnableWindow(false);
cwnd_transactionSubject->EnableWindow(false);
cwnd_content->EnableWindow(false);
cwnd_add->EnableWindow(true);
cwnd_delete->EnableWindow(false);
cwnd_modify->EnableWindow(false);
cwnd_save->EnableWindow(false);
cwnd_cancel->EnableWindow(false);
flag=0;//刚开始没有修改和增加;
//((CComboBox*)cwnd_transactionType)->AddString("会议");
//((CComboBox*)cwnd_transactionType)->AddString("接待");
//((CComboBox*)cwnd_transactionType)->SetCurSel(0);//默认以"会议"为可见;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CTransactionRecord::OnClickList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
UpdateData();
POSITION pos=m_list.GetFirstSelectedItemPosition();//获得单击的位置
if(pos)
{
int nFistSelItem=m_list.GetNextSelectedItem(pos);//获得单击条目
m_transactionSubject=m_list.GetItemText(nFistSelItem,2);//获得事务主题
}
Ctransaction transaction;
transaction.GetData(m_transactionSubject);
m_transactionType=transaction.GetTransactionType();
m_content=transaction.GetTransactionContent();
m_transactionDate=transaction.GetTransactionDate();
UpdateData(false);
GetDlgItem(IDC_DELETE)->EnableWindow(true);//选中后删除可用
GetDlgItem(IDC_MODIFY)->EnableWindow(true);//选中后修改可用
*pResult = 0;
}
void CTransactionRecord::OnDelete()
{
// TODO: Add your control notification handler code here
//提醒用户是否确定删除
if(AfxMessageBox("您确定要删除吗?",MB_YESNO,0)!=IDYES)
{
return;
}
Ctransaction transaction;
transaction.SQLdelete(m_transactionType);
Refresh();
UpdateData(false);
}
void CTransactionRecord::Refresh()
{
m_list.DeleteAllItems();
_ConnectionPtr m_pConn("ADODB.Connection");
m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\\DataSource\\office1.mdb;Persist Security Info=False","","",adConnectUnspecified);
_RecordsetPtr m_pRecordset("ADODB.Recordset");//记录集
//打开
m_pRecordset->Open("Select*From 事务表",_variant_t(m_pConn,true),adOpenDynamic,adLockPessimistic,adCmdText);
int i=0;
//循环将库中的数据读出(_bstr_t)来强制转换
while(!m_pRecordset->EndOfFile)
{
m_list.InsertItem(i,(_bstr_t)m_pRecordset->GetCollect("事务类型"));
m_list.SetItemText(i,1,(_bstr_t)m_pRecordset->GetCollect("事务日期"));
m_list.SetItemText(i,2,(_bstr_t)m_pRecordset->GetCollect("事务主题"));
i++;
m_pRecordset->MoveNext();
}
}
//DEL void CTransactionRecord::OnEditchangeTransactiontype()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL
//DEL }
void CTransactionRecord::OnAdd()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_SAVE)->EnableWindow(true);//选中后保存可用
cwnd_content->EnableWindow(true);
cwnd_transactionSubject->EnableWindow(true);
cwnd_transactionDate->EnableWindow(true);
cwnd_transactionType->EnableWindow(true);
GetDlgItem(IDC_CANCELLATION)->EnableWindow();//选中后修改可用
Ctransaction transaction;
m_content=_T("");
m_transactionType=_T("");
m_transactionDate=COleDateTime::GetCurrentTime();
m_transactionSubject=_T("");
UpdateData(false);//各个控件都可用处于编辑状态
flag=1;//置 1 为添加状态,当用户点击保存时候将写如数据库
// GetLBText()
/* transaction.SetTransactionContent(m_content);
transaction.SetTransactionDate(m_transactionDate);
transaction.SetTransactionSubject(m_transactionSubject);
transaction.SetTransactionType(m_transactionType);
transaction.SQLinsert();
/* cwnd_transactionSubject->EnableWindow(false);
cwnd_transactionDate->EnableWindow(false);
cwnd_transactionType->EnableWindow(false);
cwnd_content->EnableWindow(false);
*/
}
void CTransactionRecord::OnModify()
{
flag=2;//修改时将flag设置为2
GetDlgItem(IDC_DELETE)->EnableWindow();//选中后修改可用
GetDlgItem(IDC_SAVE)->EnableWindow();//选中后修改可用
GetDlgItem(IDC_CANCELLATION)->EnableWindow();//选中后修改可用
cwnd_content->EnableWindow(true);
cwnd_transactionSubject->EnableWindow(true);
cwnd_transactionDate->EnableWindow(true);
cwnd_transactionType->EnableWindow(true);
}
void CTransactionRecord::OnSave()
{
// TODO: Add your control notification handler code here
//处理添加和删除功能
if(flag==1)
{
//将变量更新到控件上
UpdateData();
Ctransaction transaction;
transaction.SetTransactionContent(m_content);
// m_transactionDate=COleDateTime::GetCurrentTime();
transaction.SetTransactionDate(m_transactionDate);
transaction.SetTransactionSubject(m_transactionSubject);
transaction.SetTransactionType(m_transactionType);
if(m_content==""||m_transactionType==""||m_transactionSubject=="")
{
MessageBox("有项目未填写,保存无效");
return;
}
transaction.SQLinsert();
}
if(flag==2)
{
UpdateData();
CString m_oldSubject;// 保存好原来的主题
Ctransaction transaction;
m_oldSubject=m_transactionSubject;
transaction.SetTransactionContent(m_content);
transaction.SetTransactionDate(m_transactionDate);
transaction.SetTransactionSubject(m_transactionSubject);
transaction.SetTransactionType(m_transactionType);
if(m_content==""||m_transactionType==""||m_transactionSubject=="")
{
MessageBox("有项目未填写,保存无效");
return;
}
transaction.SQLupdate(m_oldSubject);
}
//添加完成后要更新,将新的显示出来
Refresh();
//完成添加或删除任务将flag设置为0
flag=0;
cwnd_content->EnableWindow(false);
cwnd_transactionSubject->EnableWindow(false);
cwnd_transactionDate->EnableWindow(false);
cwnd_transactionType->EnableWindow(false);
GetDlgItem(IDC_SAVE)->EnableWindow(false);
GetDlgItem(IDC_CANCELLATION)->EnableWindow(false);
/* if(m_transactionSubject=="")
{
MessageBox("不能为空");
return;
}
if(flag==1)
{
MessageBox("您添加了一条记录");
}
else if(flag==2)
{
MessageBox("您正在修改了一条记录");
}
*/
}
void CTransactionRecord::OnCancellation()
{
// TODO: Add your control notification handler code here
m_content="";
m_transactionType="";
m_transactionDate=COleDateTime::GetCurrentTime();
m_transactionSubject="";
UpdateData(false);
}
/*
CListCtrl m_list;
CString m_content;
CString m_transactionType;
COleDateTime m_transactionDate;
CString m_transactionSubject;
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -