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

📄 ch22.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<P>Once you can add and delete records from a database table, you'll have a full-featured program for manipulating a flat (that is, not a relational) database. In this case, the &quot;flat database&quot; is the Employees table of the department store 
relational database. Adding and deleting records in a database table is an easier process than you might believe, thanks to Visual C++'s <font color="#008000">CRecordView</font> and <font color="#008000">CRecordSet</font> classes, which provide all the 
member functions you need to accomplish these common database tasks. You'll need to add some menu items to the application, as first discussed in <A HREF="index10.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index10.htm" target="text">Chapter 10</A>, &quot;Building a Complete Application: ShowString.&quot; 
Follow these steps to include <font color="#008000">add</font> and <font color="#008000">delete</font> commands in the Employee application:</P>

<ol>

<li><P> Select the ResourceView tab, open the Menu folder, and double-click the IDR_MAINFRAME menu ID. The menu editor appears, as shown in Figure 22.22.</P>

</ol>

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

<P><I>Developer Studio's menu editor is in the right-hand pane.</I></P>

<ol start=2>

<li><P> Click the Record menu item to open it, and click the blank menu item at the bottom of the menu. Choose <U>V</U>iew, <U>P</U>roperties and pin the Properties dialog box in place. </P>

<li><P> In the <U>I</U>D edit box, enter <B>ID_RECORD_ADD</B>, and in the <U>C</U>aption box, enter <B>&amp;Add Record</B>, as shown in Figure 22.23. This adds a new command to the Record menu.</P>

</ol>

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

<P><I>Add a menu item to add a record.</I></P>

<ol start=4>

<li><P> In the next blank menu item, add a delete command with the ID <font color="#008000">ID_RECORD_DELETE</font> and the caption <B>&amp;Delete Record</B>.</P>

</ol>

<P>Next, you will connect these commands to toolbar buttons, as first discussed in <A HREF="index11.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index11.htm" target="text">Chapter 11</A>, &quot;Toolbars and Status Bars.&#148; Follow these steps:</P>

<ol>

<li><P> In the ResourceView pane, open the Toolbar folder, and then double-click the IDR_MAINFRAME ID. The application's toolbar appears in the resource editor.</P>

<li><P> Click the blank toolbar button to select it, and then use the editor's tools to draw a blue + on the button.</P>

<li><P> Double-click the new button in the toolbar. The Toolbar Button Properties property sheet appears. Select <font color="#008000">ID_RECORD_ADD</font> in the <U>I</U>D box, as shown in Figure 22.24.</P>

</ol>

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

<P><I>Add a button and connect it to the menu item.</I></P>

<ol start=4>

<li><P> Select the blank button again and draw a red minus sign, giving the button the <font color="#008000">ID_RECORD_DELETE</font> ID, as you can see in Figure 22.25. Drag and drop the Add and Delete buttons to the left of the Help (question mark) 
button.</P>

</ol>

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

<P><I>The minus-sign button will control the Delete function.</I></P>

<P>Now that you have added the menu items and the toolbar buttons, you need to arrange for code to &quot;catch&quot; the command message that is sent when the user clicks the button or chooses the menu item. Background information on this process is in <A 
HREF="index04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index04.htm" target="text">Chapter 4</A>, &quot;Messages &amp; Commands;&quot; <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, &quot;Building a Complete Application: ShowString;&quot; and <A HREF="index10.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index10.htm" target="text">Chapter 10</A>, 
&quot;Toolbars and Status Bars.&quot; Because it is the view that is connected to the database, the view will catch these messages. Follow these steps:</P>

<ol>

<li><P> Bring up ClassWizard and select the Message Maps tab.</P>

<li><P> Set the Class <U>N</U>ame box to <font color="#008000">CEmployeeView</font>, click the <font color="#008000">ID_RECORD_ADD</font> ID in the Object <U>I</U>Ds box, and then double-click COMMAND in the Messa<U>g</U>es box. The Add Member Function 
dialog box appears, as shown in Figure 22.26.</P>

</ol>

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

<P><I>Add a function to catch the message.</I></P>

<ol start=3>

<li><P> Click the OK button to accept the default name for the new function. The function appears in the Member <U>F</U>unctions box at the bottom of the ClassWizard dialog box.</P>

<li><P> Add a member function for the <font color="#008000">ID_RECORD_DELETE</font> command in the same way. The list of functions should resemble Figure 22.27. Click OK to close ClassWizard.</P>

</ol>

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

<P><I>The new functions appear in the Member Functions box.</I></P>

<ol start=5>

<li><P> Open the EmployeeView.h file by double-clicking CEmployeeView in the ClassView pane. In the Attributes section of the class's declaration, add the following lines:</P>

<p>

<pre><font color="#008000">protected:</font></pre>

<pre><font color="#008000"> BOOL m_bAdding;</font></pre>

</p>

<li><P> Double-click the CEmployeeView constructor in ClassView to edit it, and add this line at the bottom of the function:</P>

<pre><font color="#008000"> m_bAdding = FALSE;</font></pre>

<li><P> Double-click the <font color="#008000">OnRecordAdd()</font> function and edit it so that it looks like Listing 22.1. This code is explained in the next section.</P>

<P><I>Listing 22.1&#151;</I>CEmployeeView::OnRecordAdd()</P>

<p>

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

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

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

<pre><font color="#008000">    m_bAdding = TRUE;</font></pre>

<pre><font color="#008000">    CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);</font></pre>

<pre><font color="#008000">    int result = pCtrl-&gt;SetReadOnly(FALSE);</font></pre>

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

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

</p>

<li><P> Right-click CEmployeeView in ClassView and choose Add Virtual Function. Select OnMove from the list on the left, as shown in Figure 22.28, then click Add and Edit to add the function and edit the skeleton code immediately. </P>

</ol>

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

<P><I>Override the </I><I>OnMove()</I><I> function.</I></P>

<ol start=9>

<li><P> Edit <font color="#008000">OnMove()</font> function so that it has the code in Listing 22.2. This code is explained in the next section.</P>

<P><I>Listing 22.2&#151;</I>CEmployeeView::OnMove()</P>

<p>

<pre><font color="#008000">BOOL CEmployeeView::OnMove(UINT nIDMoveCommand) </font></pre>

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

<pre><font color="#008000">     if (m_bAdding)</font></pre>

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

<pre><font color="#008000">         m_bAdding = FALSE;</font></pre>

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

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

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

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

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

<pre><font color="#008000">         CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);</font></pre>

<pre><font color="#008000">         pCtrl-&gt;SetReadOnly(TRUE);</font></pre>

<pre><font color="#008000">         return TRUE;</font></pre>

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

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

<pre><font color="#008000">         return CRecordView::OnMove(nIDMoveCommand);</font></pre>

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

</p>

<li><P> Double-click the <font color="#008000">OnRecordDelete()</font> function and edit it so that it looks like Listing 22.3. This code is explained in the next section.</P>

<P><I>Listing 22.3&#151;</I>CEmployeeView::OnRecordDelete()</P>

<p>

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

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

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

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

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

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

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

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

⌨️ 快捷键说明

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