📄 vcg15.htm
字号:
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
/////////////////////////////////////////////////////////////////////////////
// Lines deleted ...
BOOL CRecordView::OnMove(UINT nIDMoveCommand)
{
<B>// First, there are checks to make sure that we are not waiting for</B>
<B>// the datasource:</B>
if (CDatabase::InWaitForDataSource())
{
#ifdef _DEBUG
if (afxTraceFlags & 0x20)
TRACE0("Warning: ignored move request\n");
#endif // _DEBUG
return TRUE;
}
<B>// If we're not waiting for the datasource, we get the recordset.</B>
<B>// Then we check to see if we can update the current database. We</B>
<B>// then tell the recordset that we're going to edit (using Edit())</B>
<B>// the current record. We get the user's edits (if any) and then</B>
<B>// update (using Update()) the record in the recordset.</B>
CRecordset* pSet = OnGetRecordset();
if (pSet->CanUpdate())
{
pSet->Edit();
if (!UpdateData())
return TRUE;
pSet->Update();
}
<B>// The next step depends on what the move is. There are four</B>
<B>// choices: next record, previous record, first record, and</B>
<B>// last record. The switch() block manages these four choices.</B>
switch (nIDMoveCommand)
{
case ID_RECORD_PREV:
pSet->MovePrev();
if (!pSet->IsBOF())
break;
case ID_RECORD_FIRST:
pSet->MoveFirst();
break;
case ID_RECORD_NEXT:
pSet->MoveNext();
if (!pSet->IsEOF())
break;
if (!pSet->CanScroll())
{
// Clear out screen since we're sitting on EOF
pSet->SetFieldNull(NULL);
break;
}
case ID_RECORD_LAST:
pSet->MoveLast();
break;
default:
// Unexpected case value
ASSERT(FALSE);
}
<B>// Once the specified move has been made, we simply update</B>
<B>// the view's display of the record (for the user) and return.</B>
// Show results of move operation
UpdateData(FALSE);
return TRUE;
}</FONT></PRE>
<P>Listing 15.2, the CRecordView::OnMove() handler, shows what the default action for OnMove() is. In the original handler, the current record (if there is one) is updated, and then the new record (if there is one) is loaded. At OnMove()'s completion, the view is updated to reflect the changes.
<BR>
<P>Listing 15.3 shows the new OnMove() handler. Much like the default OnMove(), this version updates the current records (all 10 of them) and then loads 10 "new" records. The facility can handle only single moves at a time, so nine of the 10 reads aren't necessary and could be replaced with assignments to move the currently loaded data to the new locations.
<BR>
<P>Listing 15.3 differs from Listing 15.1 in that the OnMove() handler doesn't call other functions that have been written. (In other words, it can run on its own.)
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 15.3. The OnMove() handler.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080"><B>BOOL CContinView::OnMove(UINT nIDMoveCommand)</B>
<B>{</B>
<B>int i;</B>
<B>int nStepBack;</B>
<B>//---------START OF DBVIEW.CPP OnMove()...</B>
<B>// First, there are checks to make sure that we aren't waiting for</B>
<B>// the datasource:</B>
<B> if (CDatabase::InWaitForDataSource())</B>
<B> {</B>
<B>#ifdef _DEBUG</B>
<B> if (afxTraceFlags & 0x20)</B>
<B> TRACE0("Warning: ignored move request\n");</B>
<B>#endif // _DEBUG</B>
<B> return TRUE;</B>
<B> }</B>
<B>// If we aren't waiting for the datasource, we get the recordset.</B>
<B>// Then we check to see if we can update the current database. We</B>
<B>// then tell the recordset that we are going to edit (using Edit())</B>
<B>// the current record. We get the user's edits (if any) and then</B>
<B>// update (using Update()) the record in the recordset.</B>
<B> if (m_pSet->CanUpdate())</B>
<B> {</B>
<B> if (!UpdateData())</B>
<B> return TRUE;</B>
<B> nStepBack = 0;</B>
<B> for (i = 0; i < 10; i++)</B>
<B> {// Save the current record and then get next one!</B>
<B> m_pSet->Edit();</B>
<B> m_pSet->m_English_Name = m_EnglishName[i];</B>
<B> m_pSet->m_Product_Name = m_ProductName[i];</B>
<B> m_pSet->m_Unit_Price = m_UnitPrice[i];</B>
<B> m_pSet->m_Units_In_Stock = m_UnitsInStock[i];</B>
<B> m_pSet->m_Units_On_Order = m_UnitsOnOrder[i];</B>
<B> m_pSet->Update();</B>
<B> if (!m_pSet->IsEOF())</B>
<B> {</B>
<B> TRY</B>
<B> {// Use old-style exceptions for Visual C++ 1.5x</B>
<B> m_pSet->MoveNext();</B>
<B> --nStepBack;</B>
<B> }</B>
<B> CATCH(CDBException, e)</B>
<B> {// Died. Should use message box to user!</B>
<B> TRACE("MoveNext() fail Ret = %d Error '%s', cause '%s'\n",</B>
<B> e->m_nRetCode,</B>
<B> (const char *)e->m_strError,</B>
<B> (const char *)e->m_strStateNativeOrigin);</B>
<B> }</B>
<B> END_CATCH</B>
<B> }</B>
<B> else</B>
<B> {</B>
<B> break;</B>
<B> }</B>
<B> }</B>
<B>// Restore the record pointer! Take nStepBack giant steps back!</B>
<B> TRY</B>
<B> {// Use old-style exceptions for Visual C++ 1.5x</B>
<B> m_pSet->Move(nStepBack); // Back to original record...</B>
<B> }</B>
<B> CATCH(CDBException, e)</B>
<B> {// Died. Should use message box to user!</B>
<B> TRACE("Move(nStepBack) failed Ret = %d Error '%s', cause '%s'\n",</B>
<B> e->m_nRetCode,</B>
<B> (const char *)e->m_strError,</B>
<B> (const char *)e->m_strStateNativeOrigin);</B>
<B> }</B>
<B> END_CATCH</B>
<B> }</B>
<B>// The next step depends on what the move is. There are four</B>
<B>// choices: next record, previous record, first record, and</B>
<B>// last record. The switch() block manages these four choices.</B>
<B> switch (nIDMoveCommand)</B>
<B> {</B>
<B> case ID_RECORD_PREV:</B>
<B> m_pSet->MovePrev();</B>
<B> if (!m_pSet->IsBOF())</B>
<B> break;</B>
<B> case ID_RECORD_FIRST:</B>
<B> m_pSet->MoveFirst();</B>
<B> break;</B>
<B> case ID_RECORD_NEXT:</B>
<B> m_pSet->MoveNext();</B>
<B> if (!m_pSet->IsEOF())</B>
<B> break;</B>
<B> if (!m_pSet->CanScroll())</B>
<B> {</B>
<B> // Clear out screen since we're sitting on EOF</B>
<B> m_pSet->SetFieldNull(NULL);</B>
<B> break;</B>
<B> }</B>
<B> case ID_RECORD_LAST:</B>
<B> m_pSet->MoveLast();</B>
<B> break;</B>
<B> default:</B>
<B> // Unexpected case value</B>
<B> ASSERT(FALSE);</B>
<B> }</B>
<B>//---------END OF DBVIEW.CPP OnMove()...</B>
<B>nStepBack = 0;</B>
<B> for (i = 0; i < 10; i++)</B>
<B> {// Save the current record and then get next one!</B>
<B> m_EnglishName[i] = m_pSet->m_English_Name;</B>
<B> m_ProductName[i] = m_pSet->m_Product_Name;</B>
<B> m_UnitPrice[i] = m_pSet->m_Unit_Price;</B>
<B> m_UnitsInStock[i] = m_pSet->m_Units_In_Stock;</B>
<B> m_UnitsOnOrder[i] = m_pSet->m_Units_On_Order;</B>
<B> if (!m_pSet->IsEOF())</B>
<B> {</B>
<B> TRY</B>
<B> {// Use old-style exceptions for Visual C++ 1.5x</B>
<B> m_pSet->MoveNext();</B>
<B> --nStepBack;</B>
<B> }</B>
<B> CATCH(CDBException, e)</B>
<B> {// Died. Should use message box to user!</B>
<B> TRACE("MoveNext() failed Ret = %d Error '%s', cause '%s'\n",</B>
<B> e->m_nRetCode,</B>
<B> (const char *)e->m_strError,</B>
<B> (const char *)e->m_strStateNativeOrigin);</B>
<B> }</B>
<B> END_CATCH</B>
<B> }</B>
<B> else</B>
<B> {</B>
<B> m_pSet->SetFieldNull(NULL);</B>
<B> }</B>
<B> }</B>
<B>// Restore the record pointer! Take nStepBack giant steps back!</B>
<B> TRY</B>
<B> {// Use old-style exceptions for Visual C++ 1.5x</B>
<B> m_pSet->Move(nStepBack);</B>
<B> }</B>
<B> CATCH(CDBException, e)</B>
<B> {// Died. Should use message box to user!</B>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -