📄 cardfiledlg.cpp
字号:
//Use the namespace identifier to avoid conflict with any other
//init functions that may exist.
dbAx::Init();
//Create the connection object
m_Cn.Create();
//Create the Connection events object on the heap. We don't need
//to worry with deleting the Events object since this is handled
//internally by its Release function. When no longer needed, the
//Events object deletes itself.
m_Cn._SetConnectionEvents(new CCardFileEvents);
//Set the cursor location and open the database connection
m_Cn.CursorLocation(adUseClient);
m_Cn.Open((LPCTSTR)szConnect);
//Create the AccountSet, Recordset events, and Open
//Note that options are set on the CAxRecordset object
//before callint the Open method. In this instance,
//the Create method must be called first. See the
//note below regarding m_ContactSet.
m_AccountSet.Create();
m_AccountSet.CursorType(adOpenDynamic);
m_AccountSet.CacheSize(50);
m_AccountSet._SetRecordsetEvents(new CAccountSetEvents);
m_AccountSet.Open(_T("Select * From Account"), &m_Cn);
//Set the marshal options to minimize records returned to server
//to only those that have been edited.
m_AccountSet.MarshalOptions(adMarshalModifiedOnly);
//Setup the command object that will execute the stored
//procedure "get_Contacts" to match contact information
//with the currently selected Account record.
m_ContactCmd.Create();
m_ContactCmd.ActiveConnection(&m_Cn);
m_ContactCmd.CommandText(_T("[get_Contacts]"));
m_ContactCmd.CommandType(adCmdStoredProc);
m_ContactCmd.m_szAccntIDParam = _T("A"); //Initial dummy value
//Open the Contact recordset based on the Contact Command object.
//An open recordset may be updated by setting the associated command
//object's parameter(s) and calling the recordset's Requery() method.
//A recordset set opened directly can also use Requery. This has the
//same effect as closing and reopening the recordset.
//Note that since there are no calls to any of the recordset object's
//methods prior to the Open statement, it unnecessary to make a call
//to the Create method. This is handled automatically when opening
//the recordset.
m_ContactSet.Open(&m_ContactCmd, adOpenDynamic);
}
catch ( dbAx::CAxException *e )
{
MessageBox(e->m_szErrorDesc, _T("CardFile Message"), MB_OK);
delete e;
return (FALSE);
}
return (TRUE);
}
//Create the columns for the Account and Contact lists in
//the main dialog
void CCardFileDlg::InitListColumns()
{
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
lvc.fmt = LVCFMT_LEFT;
lvc.cchTextMax = 64;
CRect rc;
int nCol = 0;
m_ctrlAccountList.GetClientRect(&rc);
lvc.cx = (int)(rc.right * 0.35);
lvc.pszText = _T("Account");
m_ctrlAccountList.InsertColumn(nCol++, &lvc);
lvc.cx = (int)(rc.right * 0.65);
lvc.pszText = _T("Name");
m_ctrlAccountList.InsertColumn(nCol++, &lvc);
//Setup columns in Contact list control
nCol = 0;
lvc.cx = (int)(rc.right * 0.25);
lvc.pszText = _T("Name");
m_ctrlContactList.InsertColumn(nCol++, &lvc);
lvc.pszText = _T("Title");
m_ctrlContactList.InsertColumn(nCol++, &lvc);
lvc.pszText = _T("Phone");
m_ctrlContactList.InsertColumn(nCol++, &lvc);
lvc.pszText = _T("Email");
m_ctrlContactList.InsertColumn(nCol++, &lvc);
m_ctrlAccountList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
m_ctrlContactList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
}
//BuildAccountList will populate the Account list with all
//available records. This method is called on startup and
//each time a new Account record is added, edited, or deleted
void CCardFileDlg::BuildAccountList()
{
m_ctrlAccountList.DeleteAllItems();
try
{
if ( m_AccountSet._IsEmpty() )
{
m_ctrlAccountList.InsertItem(0, _T("<< >>"));
return;
}
m_ctrlAccountList.SetItemCount(m_AccountSet.RecordCount());
int iItem = 0;
m_AccountSet.MoveFirst();
while ( !m_AccountSet.IsEOF() )
{
m_ctrlAccountList.InsertItem(iItem, m_AccountSet.m_szAccountID);
m_ctrlAccountList.SetItemText(iItem, 1, m_AccountSet.m_szName);
iItem++;
m_AccountSet.MoveNext();
}
//Highlight the first item
LV_ITEM lvi;
lvi.mask = LVIF_TEXT;
lvi.iItem = 0;
lvi.iSubItem = 0;
lvi.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
lvi.state = LVIS_SELECTED | LVIS_FOCUSED;
lvi.pszText = (LPWSTR)(LPCTSTR)m_AccountSet.m_szAccountID;
lvi.iImage = 0;
lvi.iIndent = 0;
lvi.cchTextMax = 50;
m_AccountSet.MoveFirst();
m_ctrlAccountList.SetItemState(lvi.iItem, &lvi);
}
catch ( dbAx::CAxException *e )
{
MessageBox(e->m_szErrorDesc, _T("CardFile Message"), MB_OK);
delete e;
}
}
//BuildContactList populates the contact list in response to
//a selection change in the Account list. It is also called
//each time a contact record is added, edited or deleted.
void CCardFileDlg::BuildContactList()
{
m_ctrlContactList.DeleteAllItems();
try
{
m_ContactCmd.m_szAccntIDParam = m_AccountSet.m_szAccountID;
m_ContactSet.Requery();
if ( m_ContactSet._IsEmpty() )
{
m_ctrlContactList.InsertItem(0, _T("<< >>"));
}
else
{
m_ctrlContactList.SetItemCount(m_ContactSet.RecordCount());
int iItem = 0;
m_ContactSet.MoveFirst();
while ( !m_ContactSet.IsEOF() )
{
m_ctrlContactList.InsertItem(iItem, m_ContactSet.m_szName);
m_ctrlContactList.SetItemText(iItem, 1, m_ContactSet.m_szTitle);
m_ctrlContactList.SetItemText(iItem, 2, m_ContactSet.m_szPhone);
m_ctrlContactList.SetItemText(iItem, 3, m_ContactSet.m_szEmail);
iItem++;
m_ContactSet.MoveNext();
}
}
}
catch ( dbAx::CAxException *e )
{
MessageBox(e->m_szErrorDesc, _T("CardFile Message"), MB_OK);
delete e;
}
}
//Display the Account dialog where a new account record is added
void CCardFileDlg::OnBnClickedAddAccount()
{
CAccountDlg dlg(this);
if ( dlg.DoModal() == IDOK )
BuildAccountList();
}
//Display the Account dialog where the current record is edited
void CCardFileDlg::OnBnClickedEditAccount()
{
CAccountDlg dlg(TRUE, this);
if ( dlg.DoModal() == IDOK )
BuildAccountList();
}
//Delete the current Account record. This method demonstrates the
//AxLib Execute procedure where an SQL statement can be issued
//directly to the data provider
void CCardFileDlg::OnBnClickedDeleteAccount()
{
CString szMsg;
szMsg.Format(_T("Delete %s?"), m_AccountSet.m_szAccountID);
MessageBeep(MB_ICONEXCLAMATION);
int Reply = AfxMessageBox(szMsg, MB_YESNO);
try
{
if ( Reply == IDYES )
{
//Delete the current Account and all
//related Contact information
CString szExecStr;
szExecStr.Format(_T("Delete From Contact Where AccountID = '%s'"),
m_AccountSet.m_szAccountID);
m_AccountSet.Delete();
m_Cn.Execute(szExecStr);
BuildAccountList();
}
}
catch ( dbAx::CAxException *e )
{
MessageBox(e->m_szErrorDesc, _T("CardFile Message"), MB_OK);
delete e;
}
}
//Display the Contact dialog where a new contact record is added
void CCardFileDlg::OnBnClickedAddContact()
{
CContactDlg dlg(this);
if ( dlg.DoModal() == IDOK )
BuildContactList();
}
//Display the Contact dialog where the current record is edited
void CCardFileDlg::OnBnClickedEditContact()
{
CContactDlg dlg(TRUE, this);
if ( dlg.DoModal() == IDOK )
BuildContactList();
}
//Delete the current contact record
void CCardFileDlg::OnBnClickedDeleteContact()
{
CString szMsg;
szMsg.Format(_T("Delete %s?"), m_ContactSet.m_szName);
MessageBeep(MB_ICONEXCLAMATION);
int Reply = AfxMessageBox(szMsg, MB_YESNO);
if ( Reply == IDYES )
{
m_ContactSet.Delete();
BuildContactList();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -