📄 wwwtc7.htm
字号:
<HTML>
<HEAD>
<TITLE> What's Wrong With This Code? Volume #7: Phantom Updates</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H2>
What's Wrong With This Code? Volume #7
</H2>
<H4>
Phantom Updates
</H4>
<P>
In 1998, I started working on a medium sized database program using C++Builder 3 and MS SQL Server 6.5. When C++Builder 4 came out in 1999,
we decided to postpone upgrading because we were about to ship the first version of the product. After releasing version 1.0 of our system,
we started thinking about upgrading to C++Builder 4.
</P>
<P>
Our program is a front end for the database. The primary form in our program contains a couple of panels. The panels are separated by a
splitter control. Each panel displays data from the database. The form also contains a toolbar with buttons on it for editing and updating
the data. Figure 1 shows a stripped down version of the main form.
</P>
<IMG SRC="images/splitterbug.gif" alt="database form" BORDER=0 ALIGN="BOTTOM" width="498" height="299"> <BR>
<H4>Figure 1. The primary form in my database application </H4>
<P>
When the user presses the update button on the toolbar, an <TT>OnClick</TT> handler fires. Over the years, I have learned that it is not
wise to place tons of code in an <TT>OnClick</TT> handler, because doing so can make it difficult to maintain a project. With this in mind,
I added a private method to my class called <TT>Update</TT>. The code looks like this:
</P>
<pre>
<font color="navy">//-----------------------------------------------------------------</font>
<font color="navy">// MAINFORM.H Header file</font>
<font color="navy">//</font>
<b>class</b> TForm1 <b>:</b> <b>public</b> TForm
<b>{</b>
<b>__published</b><b>:</b>
<font color="navy">// ...</font>
TSpeedButton <b>*</b>btnUpdate<b>;</b>
<b>void</b> <b>__fastcall</b> btnUpdateClick<b>(</b>TObject <b>*</b>Sender<b>)</b><b>;</b>
<b>private</b><b>:</b>
<b>void</b> <b>__fastcall</b> Update<b>(</b><b>)</b><b>;</b>
<b>public</b><b>:</b>
<b>__fastcall</b> TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b><b>;</b>
<b>}</b><b>;</b>
<font color="navy">//-----------------------------------------------------------------</font>
<font color="navy">//-----------------------------------------------------------------</font>
<font color="navy">// MAINFORM.CPP Source file</font>
<font color="navy">//</font>
<b>__fastcall</b> TForm1<b>:</b><b>:</b>TForm1<b>(</b>TComponent<b>*</b> Owner<b>)</b>
<b>:</b> TForm<b>(</b>Owner<b>)</b>
<b>{</b>
<b>}</b>
<font color="navy">//-----------------------------------------------------------------</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>btnUpdateClick<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
Update<b>(</b><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//-----------------------------------------------------------------</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>Update<b>(</b><b>)</b>
<b>{</b>
<font color="navy">// update database</font>
<font color="navy">// ...</font>
ShowMessage<b>(</b><font color="blue">"Database Updated!"</font><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//-----------------------------------------------------------------</font>
</pre>
<P>
I have stripped out all unecessary code to keep things simple. When the user presses the update button, the <TT>OnClick</tt> handler
simply calls the <TT>Update</TT> member function. <TT>Update</TT> interacts with a business object to write changes back to the
SQL database.
</P>
<P>
This code worked fine in C++Builder 3, but we noticed a bit a strangeness when we moved to C++Builder 4.
The update button still works in C++Builder 4, which is good. However, when the user moves the splitter bar, the program acts
as if the user had clicked the update button. Moving the splitter bar updates the database! How can that be? The splitter bar did
not trigger the update when we were compiling with C++Builder 3. Is the splitter bar in C++Builder 4 completely whacked? Did the
DFM file get messed up when we moved from C++Builder 3 to C++Builder 4?
</P>
<P>
See if you can determine why moving the splitter bar triggers an update when the project is compiled with C++Builder 4. You can download
the project using the links at the bottom.
</P>
<H4>
<A TARGET=_top href="wwwtc7answer.htm">Answer</A>
</H4>
<BR>
<TABLE BORDER=1 CELLPADDING=10 CELLSPACING=0 WIDTH="100%">
<TR> <TD colspan = 2><B>Code for this edition</B> </TD> </TR>
<TR> <TD><TT><a HREF="download/wwwtc7.zip">wwwtc7.zip</a></TT></TD><TD>BCB4 project that contains problem code.</TD> </TR>
<TR> <TD><TT><a HREF="download/wwwtc7x.zip">wwwtc7x.zip</a></TT></TD><TD>Same project, includes an EXE (230 kb).</TD> </TR>
</TABLE>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -