📄 ch11.htm
字号:
<DL>
<DT></DT>
</DL>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>If you are concerned that the solution
to the problem in the last
paragraph requires using three <TT>TTable</TT> objects
instead of only two, I would ask you to recall that the goal of this book is to show
how to get things done, not how to do things in the smallest possible space. If you
can get things done in
five minutes through a technique that you know is bug free,
that is something you should give up only reluctantly, particularly if the alternative
is working for days or weeks to implement a solution that is likely to have bugs
that will take
another week or two to squash. At the very least, you should implement
the quick solution for the first draft of your application, and then come back and
look for optimizations once you have a working version of the product. <BR>
<BR>
Don't ever
try to optimize during your first draft of an application! There is no
such thing as getting things right the first time in programming. Instead, you should
implement a reasonable solution, critique it, come back and make improvements, critique
the
improvements, make another pass over the application, and so on. This kind of
cycle demands that you not get too hung up on optimizations during early drafts,
because you are likely to find that any one part of the application will change in
future
revisions. If you have the thing implemented correctly, and there is still
time left in the project cycle, then you can come back and seek to optimize the code!
<BR>
<BR>
The final kicker in this analysis is that contemporary application
programmers rarely
have time to optimize. Given the success rate of most projects, your customers or
managers will usually be ecstatic if you just turn in a working solution to the problem
on time. If you release the same application 10 percent
faster and 20 percent smaller,
but six months later, it's unlikely you will win quite the same number of kudos you
think you deserve. I usually leave the minute optimizations up to the development
teams at Borland. They know how to reach into the
fire without getting burned.
<HR>
</BLOCKQUOTE>
<P>If you want to change the author associated with a particular record, you just
click a new item in the list box. The author number will be changed automatically
for you by the lookup. It's all
very simple and intuitive when viewed from the user's
perspective.
<H3><A NAME="Heading26"></A><FONT COLOR="#000077">TDBGrid at Runtime</FONT></H3>
<P><TT>TDBGrid</TT> objects can be completely reconfigured at runtime. You can hide
and show columns,
change the order of columns, the color of columns, the color of
rows, the color of fields, and the width of columns.</P>
<P>The GridTricks program, shown in Figure 11.9, demonstrates how to take a <TT>TDBGrid</TT>
through its paces at runtime. The
program is fairly straightforward except for two
brief passages. The first passage involves creating checkbox controls on-the-fly,
and the second shows how to change the traits of columns.<BR>
<BR>
<A NAME="Heading27"></A><A
HREF="11ebu09.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/11/11ebu09.jpg">FIGURE 11.9.</A><FONT COLOR="#000077">
</FONT><I>The main GridTricks program enables you to change the appearance of a grid
at runtime. <BR>
</I><BR>
You need a color monitor to really see what is happening.</P>
<P>When the
user wants to decide which fields are visible, GridTricks pops up a second
form and displays the names of all the fields from the <TT>ORDERS</TT> table in a
series of checkboxes. The user can then select the fields that he or she wants to
make
visible. The selected checkboxes designate fields that are visible, whereas
the nonselected ones represent invisible fields. The program also enables you to
set the order and width of fields, as well as to hide and show the titles at the
top of the
grid. (See Listings 11.1 and 11.2.) The code for the GridTricks program
is in the <TT>CHAP17</TT> directory on this book's CD-ROM. (See Listings 11.4-11.8.)<BR>
<BR>
<A NAME="Heading28"></A><FONT COLOR="#000077"><B>Listing 11.4. The main unit for
the
GridTricks Program.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">///////////////////////////////////////
// File: Main.cpp
// Project: GridTricks
// Copyright (c) 1997 by Charlie Calvert
#include <vcl\vcl.h>
#pragma hdrstop
#include
"Main.h"
#include "DMod1.h"
#include "NamesDlg.h"
#include "ColumnEditor1.h"
#include "ShowOptions1.h"
#define NEWCOLOR clGreen
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall
TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Close();
}
void __fastcall TForm1::FieldNames1Click(TObject *Sender)
{
NamesDialog->ShowNames(fdFieldNames);
}
void
__fastcall TForm1::FieldObjectNames1Click(TObject *Sender)
{
NamesDialog->ShowNames(fdObjectNames);
}
///////////////////////////////////////
// DBGrid1DrawColumnCell
// Paints ROW different color depending on value of ItemsTotal field
///////////////////////////////////////
void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,
const TRect &Rect, Integer DataCol, TColumn *Column,
TGridDrawState State)
{
if (ColorRows1->Checked)
{
if
(DMod->OrdersTableItemsTotal->Value < 1000)
DBGrid1->Canvas->Font->Color = clRed;
else if (DMod->OrdersTableItemsTotal->Value < 10000)
DBGrid1->Canvas->Font->Color = clBlue;
else
DBGrid1->Canvas->Font->Color = clGreen;
}
DBGrid1->DefaultDrawColumnCell(Rect, DataCol, Column, State);
}
void TForm1::ColorTitles(BOOL UseDefaultColor)
{
TColor Colors[] = {clRed, clBlue, clGreen, clLime, clWhite,
clFuchsia};
int i;
for (i = 0; i < DBGrid1->Columns->Count; i++)
{
TColumn *Column = DBGrid1->Columns->Items[i];
TColumnTitle *ColumnTitle = Column->Title;
if (UseDefaultColor)
ColumnTitle->Font->Color = FDefaultColor;
else
ColumnTitle->Font->Color = Colors[random(7)];
}
}
void __fastcall TForm1::AnimateTitles1Click(TObject *Sender)
{
Timer1->Enabled = (!Timer1->Enabled);
AnimateTitles1->Checked = Timer1->Enabled;
if (!AnimateTitles1->Checked)
ColorTitles(True);
}
void __fastcall TForm1::ColorRows1Click(TObject *Sender)
{
ColorRows1->Checked = (!ColorRows1->Checked);
DBGrid1->Repaint();
}
void __fastcall TForm1::MarkColumnClick(TObject *Sender)
{
MarkColumn->Checked = (!MarkColumn->Checked);
TColumn *Column = DBGrid1->Columns->Items[DBGrid1->SelectedIndex];
if (MarkColumn->Checked)
{
Column->Font->Color = NEWCOLOR;
Column->Font->Style = TFontStyles() << fsBold;
}
else
{
Column->Font->Color = FDefaultColor;
Column->Font->Style = TFontStyles();
}
HandleCaption();
}
///////////////////////////////////////
// Handle Caption
///////////////////////////////////////
void TForm1::HandleCaption()
{
TColumn *Column = DBGrid1->Columns->Items[DBGrid1->SelectedIndex];
AnsiString
S(DBGrid1->SelectedIndex);
Caption = S;
if (Column->Font->Color == FDefaultColor)
Caption = "Column " + S + " is Default";
else
Caption = "Column " + S + " is Marked";
}
void
__fastcall TForm1::DBGrid1ColEnter(TObject *Sender)
{
TColumn *Column = DBGrid1->Columns->Items[DBGrid1->SelectedIndex];
MarkColumn->Checked = (Column->Font->Color == NEWCOLOR);
HandleCaption();
}
void __fastcall
TForm1::FormCreate(TObject *Sender)
{
FDefaultColor = DBGrid1->Font->Color;
HandleCaption();
}
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
ColorTitles(False);
}
void __fastcall TForm1::ShowFieldEditor1Click(TObject
*Sender)
{
ColumnEditor->ShowColumns();
}
void __fastcall TForm1::ToggleTitles1Click(TObject *Sender)
{
if (DBGrid1->Options.Contains(dgTitles))
DBGrid1->Options = TDBGridOptions(DBGrid1->Options) >> dgTitles;
else
DBGrid1->Options = TDBGridOptions(DBGrid1->Options) << dgTitles;
}
void __fastcall TForm1::ToggleIndicator1Click(TObject *Sender)
{
if (DBGrid1->Options.Contains(dgIndicator))
DBGrid1->Options =
TDBGridOptions(DBGrid1->Options) >> dgIndicator;
else
DBGrid1->Options = TDBGridOptions(DBGrid1->Options) << dgIndicator;
}
void __fastcall TForm1::ShowTitlesIndicator1Click(TObject *Sender)
{
ShowTitlesIndicator1->Checked =
!(TDBGridOptions(DBGrid1->Options).Contains(dgIndicator) &&
TDBGridOptions(DBGrid1->Options).Contains(dgTitles));
if (ShowTitlesIndicator1->Checked)
DBGrid1->Options =
TDBGridOptions(DBGrid1->Options) << dgIndicator << dgTitles;
else
DBGrid1->Options = TDBGridOptions(DBGrid1->Options) >> dgIndicator >> dgTitles;
DBGrid1->Refresh();
}
void __fastcall
TForm1::ColLines1Click(TObject *Sender)
{
if (DBGrid1->Options.Contains(dgColLines))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -