⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch22.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<pre><font color="#008000"> </font><font color="#008000"> </font></pre>

<pre><font color="#008000"> </font><font color="#008000"> UpdateData(FALSE);</font></pre>

<pre><font color="#008000">}</font></pre>

</p>

</ol>

<P>You've now modified the Employee application so that it can add and delete&#151;as well as update&#151;records. After compiling the application, run it by selecting the <U>B</U>uild, E<U>x</U>ecute command from Developer Studio's menu bar or by 
pressing Ctrl+F5. When you do, you see the Employee application's main window, which doesn't look any different than it did in the previous section. Now, however, you can add new records by clicking the Add button on the toolbar (or by selecting the 
<U>R</U>ecord, <U>A</U>dd Record command in the menu bar) and delete records by clicking the Delete button (or by clicking the <U>R</U>ecord, <U>D</U>elete Record command).</P>

<P>When you click the Add button, the application displays a blank record. Fill in the fields for the record; then when you move to another record, the application automatically updates the database with the new record. To delete a record, just click the 
Delete button. The current record (the one on the screen) vanishes and is replaced by the next record in the database.</P>

<P><A ID="I13" NAME="I13"><B>Examining the </B><B><I>OnRecordAdd()</I></B><B> Function</B></A></P>

<P>You now may be wondering how the C++ code you added to the application works. <font color="#008000">OnRecordAdd()</font> starts with a call to the <font color="#008000">AddNew()</font> member function of <font color="#008000">CEmployeeSet</font>, the 
class derived from <font color="#008000">CRecordSet</font>. This sets up a blank record for the user to fill in, but the new blank record doesn't appear on the screen until the view window's <font color="#008000">UpdateData()</font> function is called. 
Before that happens, you have a few other things to tackle.</P>

<P>After the user has created a new record, the database will need to be updated. By setting a flag in this routine, the move routine will be able to determine whether the user is moving away from an ordinary database record or a newly added one. That's 
why <font color="#008000">m_bAdding</font> is set to <font color="#008000">TRUE</font> here.</P>

<P>Now, because the user is entering a new record, it should be possible to change the contents of the Employee ID field, which is currently set to read-only. To change the read-only status of the control, the program first gets a pointer to the control 
with <font color="#008000">GetDlgItem()</font> and then calls the control's <font color="#008000">SetReadOnly()</font> member function to set the read-only attribute to <font color="#008000">FALSE</font>.</P>

<P>Finally, the call to <font color="#008000">UpdateData()</font> will display the new blank record.</P>

<P><A ID="I14" NAME="I14"><B>Examining the </B><B><I>OnMove()</I></B><B> Function</B></A></P>

<P>Now that the user has a blank record on the screen, it is a simple matter to fill in the edit controls with the necessary data. To actually add the new record to the database, the user most move to a new record, an action that forces a call to the view 
window's <font color="#008000">OnMove()</font> member function. Normally, <font color="#008000">OnMove()</font> does nothing more than display the next record. Your override will save new records as well.</P>

<P>When <font color="#008000">OnMove()</font> is called, the first thing the program does is check the Boolean variable <font color="#008000">m_bAdding</font> in order to see whether the user is in the process of adding a new record. If <font 
color="#008000">m_bAdding</font> is <font color="#008000">FALSE</font>, the body of the <font color="#008000">if</font> statement is skipped and the <font color="#008000">else</font> clause is executed. In the <font color="#008000">else</font> clause, the 
program calls the base class (<font color="#008000">CRecordView</font>) version of <font color="#008000">OnMove()</font>, which simply moves to the next record.</P>

<P>If <font color="#008000">m_bAdding</font> is <font color="#008000">TRUE</font>, the body of the <font color="#008000">if</font> statement is executed. There, the program first resets the <font color="#008000">m_bAdding</font> flag, then calls <font 
color="#008000">UpdateData()</font> to transfer data out of the view window's controls and into the recordset. A call to the recordset's <font color="#008000">CanUpdate()</font> method determines if it's okay to update the data source, after which a call 
to the recordset's <font color="#008000">Update()</font> member function adds the new record to the data source.</P>

<P>In order to rebuild the recordset, the program must call the recordset's <font color="#008000">Requery()</font> member function, and then a call to the view window's <font color="#008000">UpdateData()</font> member function transfers new data to the 
window's controls. Finally, the program sets the Employee ID field back to read-only, with another call to <font color="#008000">GetDlgItem()</font> and to <font color="#008000">SetReadOnly()</font>.</P>

<P><A ID="I15" NAME="I15"><B>Examining the </B><B><I>OnRecordDelete()</I></B><B> Function</B></A></P>

<P>Deleting a record is quite simple. <font color="#008000">OnRecordDelete()</font> just calls the recordset's <font color="#008000">Delete()</font> function. Once the record is deleted, a call to the recordset's <font color="#008000">MoveNext()</font> 
arranges for the record that follows to be displayed.</P>

<P>A problem might arise, though, when the deleted record was in the last position or when the deleted record was the only record in the recordset. A call to the recordset's <font color="#008000">IsEOF()</font> function will determine whether the 
recordset was at the end. If the call to <font color="#008000">IsEOF()</font> returns <font color="#008000">TRUE</font>, the recordset needs to be repositioned on the last record. The recordset's <font color="#008000">MoveLast()</font> function takes care 
of this task.</P>

<P>When all records have been deleted from the recordset, the record pointer will be at the beginning of the set. The program can test for this situation by calling the recordset's <font color="#008000">IsBOF()</font> function. If this function returns 
<font color="#008000">TRUE</font>, the program sets the current record's fields to <font color="#008000">NULL</font>.</P>

<P>Finally, the last task is to update the view window's display with another call to <font color="#008000">UpdateData()</font>.</P>

<P><A ID="I16" NAME="I16"><B>Sorting and Filtering</B></A></P>

<P>In many cases when you're accessing a database, you want to change the order in which the records are presented, or you may even want to search for records that fit certain criteria. MFC's ODBC database classes feature member functions that enable you 
to sort a set of records on any field. You can also call member functions in order to limit the records displayed to those whose fields contain given information, such as a specific name or ID. This latter operation is called <I>filtering</I>. In this 
section, you'll add sorting and filtering to the Employee application. Just follow these steps:</P>

<ol>

<li><P> Add a Sort menu to the application's menu bar, as shown in Figure 22.29. Let Developer Studio set the command IDs.</P>

</ol>

<A HREF="WfigC29.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC29.gif"><b>Fig. 22.29</b></A>

<P><I>The Sort menu has four commands for sorting the database.</I></P>

<ol start=2>

<li><P> Use ClassWizard to arrange for <font color="#008000">CEmployeeView</font> to catch the four new sorting commands, using the function names suggested by ClassWizard. Figure 22.30 shows the resultant ClassWizard property sheet.</P>

</ol>

<A HREF="WfigC30.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC30.gif"><b>Fig. 22.30</b></A>

<P><I>After you add the four new functions, ClassWizard should look like this.</I></P>

<ol start=3>

<li><P> Add a Filter menu to the application's menu bar, as shown in Figure 22.31. Let Developer Studio set the command IDs.</P>

</ol>

<A HREF="WfigC31.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC31.gif"><b>Fig. 22.31</b></A>

<P><I>The Filter menu has four commands.</I></P>

<ol start=4>

<li><P> Use ClassWizard to arrange for <font color="#008000">CEmployeeView</font> to catch the four new filtering commands, using the function names suggested by ClassWizard. </P>

<li><P> Create a new dialog box by choosing <U>I</U>nsert, <U>R</U>esource and double-clicking Dialog, then edit the dialog so it resembles the dialog box shown in Figure 22.32. Give the edit control the ID <B>ID_FILTERVALUE</B>.</P>

</ol>

<A HREF="WfigC32.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC32.gif"><b>Fig. 22.32</b></A>

<P><I>Create a filter dialog box.</I></P>

<ol start=6>

<li><P> Start ClassWizard while the new dialog box is on the screen. The Adding a Class dialog box appears. Select the <U>C</U>reate a New Class option and click OK.</P>

<li><P> The Create New Class dialog box appears. In the <U>N</U>ame box, type <B>CFilterDlg</B>, as shown in Figure 22.33.</P>

</ol>

<A HREF="WfigC33.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch22/WfigC33.gif"><b>Fig. 22.33</b></A>

<P><I>Create a dialog class for the Filter dialog box.</I></P>

<ol start=8>

<li><P> Click ClassWizard's Member Variables tab. Connect the <font color="#008000">IDC_FILTERVALUE</font> control to a member variable called <font color="#008000">m_filterValue</font>. Click the OK button to dismiss ClassWizard.</P>

</ol>

<P>Now that the menus and dialogs have been created and connected to skeleton functions, it's time to add some code to those functions. Double-click <font color="#008000">OnSortDepartment</font>() in ClassView and edit it to look like Listing 22.4.</P>

<P><I>Listing 22.4&#151;</I>CEmployeeView::OnSortDepartment()</P>

<pre><font color="#008000">void CEmployeeView::OnSortDepartment() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Close();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;m_strSort = &quot;DeptID&quot;;</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Open();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> UpdateData(FALSE);</font></pre>

<pre><font color="#008000">}</font></pre>

<P>Double-click <font color="#008000">OnSortID()</font> in ClassView and edit it to look like Listing 22.5. Double-click <font color="#008000">OnSortName()</font> in ClassView and edit it to look like Listing 22.6. Double-click <font 
color="#008000">OnSortRate()</font> in ClassView and edit it to look like Listing 22.7.</P>

<P><I>Listing 22.5&#151;</I>CEmployeeView::OnSortId()</P>

<pre><font color="#008000">void CEmployeeView::OnSortId() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Close();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;m_strSort = &quot;EmployeeID&quot;;</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Open();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> UpdateData(FALSE);</font><font color="#008000"> </font></pre>

<pre><font color="#008000">}</font><font color="#008000"> </font></pre>

<P><I>Listing 22.6&#151;</I>CEmployeeView::OnSortName()</P>

<pre><font color="#008000">void CEmployeeView::OnSortName() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Close();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;m_strSort = &quot;EmployeeName&quot;;</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Open();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> UpdateData(FALSE);</font></pre>

<pre><font color="#008000">}</font></pre>

<P><I>Listing 22.7&#151;LST14_07.TXT: Code for the </I>OnSortRate()<I> Function</I></P>

<pre><font color="#008000">void CEmployeeView::OnSortRate() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Close();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;m_strSort = &quot;EmployeeRate&quot;;</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> m_pSet-&gt;Open();</font></pre>

<pre><font color="#008000"> </font><font color="#008000"> UpdateData(FALSE);</font></pre>

<pre><font color="#008000">}</font></pre>

<P>At the top of EmployeeView.cpp, add the following line after the other <font color="#008000">#include</font> directives:</P>

<pre><font color="#008000">#include &quot;FilterDlg.h&quot;</font></pre>

<P>Edit <font color="#008000">OnFilterDepartment()</font>,<font color="#008000">OnFilterID()</font>, <font color="#008000">OnFilterName()</font>, and <font color="#008000">OnFilterRate()</font> using Listing 22.8.</P>

<P><I>Listing 22.8&#151;The Four Filtering Functions</I></P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -