Quick Start: C Builder 5
Quick Start: C Builder 5
Borland®
C++Builder 5 ™
Chapter 3 Chapter 5
Creating a text editor—a tutorial 3-1 Customizing the environment 5-1
Starting a new application . . . . . . . . . . . . 3-1 Organizing your work area . . . . . . . . . . . . 5-1
Setting property values . . . . . . . . . . . . . . 3-3 Docking tool windows . . . . . . . . . . . . . 5-1
Adding objects to the form . . . . . . . . . . . . 3-3 Arranging menus and toolbars . . . . . . . . 5-4
Adding support for a menu and a toolbar . . . 3-6 Customizing desktop settings . . . . . . . . . . . 5-5
Adding actions to the action list . . . . . . . 3-8 Setting default project options. . . . . . . . . . . 5-5
Adding standard actions to the action list . . 3-10 Specifying default projects and forms . . . . . . 5-5
Adding images to the image list . . . . . . . 3-11 Setting tool preferences. . . . . . . . . . . . . . . 5-6
Adding a menu. . . . . . . . . . . . . . . . . . . 3-13 Customizing the Code editor . . . . . . . . . 5-6
Clearing the text area (optional) . . . . . . . 3-15 Customizing the Form Designer. . . . . . . . 5-6
Adding a toolbar . . . . . . . . . . . . . . . . . . 3-16 Setting ClassExplorer options . . . . . . . . . 5-7
Writing event handlers . . . . . . . . . . . . . . 3-17 Customizing the Component palette . . . . . . . 5-7
Creating an event handler for the New Arranging the Component palette. . . . . . . 5-8
command . . . . . . . . . . . . . . . . . . . 3-17 Installing components . . . . . . . . . . . . . 5-8
Adding ActiveX controls . . . . . . . . . . 5-8
i
Creating component templates . . . . . . . . 5-8 Data Dictionary . . . . . . . . . . . . . . . 6-4
Customizing the Help system . . . . . . . . . . 5-9 Types of development projects . . . . . . . . . . 6-4
Applications and servers . . . . . . . . . . . . 6-4
Chapter 6 DLLs . . . . . . . . . . . . . . . . . . . . . . . 6-4
Programming with C++Builder 6-1 Custom components and packages . . . . . . 6-5
Development tools and features . . . . . . . . . 6-1 Frames . . . . . . . . . . . . . . . . . . . . . . 6-5
Using the VCL . . . . . . . . . . . . . . . . . 6-1 COM and ActiveX. . . . . . . . . . . . . . . . 6-5
Exception handling . . . . . . . . . . . . . 6-2 Type libraries . . . . . . . . . . . . . . . . . 6-6
Database connectivity and utilities . . . . . . 6-3 Deploying applications. . . . . . . . . . . . . . . 6-6
BDE Administrator . . . . . . . . . . . . . 6-3 Internationalizing applications . . . . . . . . . . 6-6
SQL Explorer (Database Explorer) . . . . 6-3
Database Desktop. . . . . . . . . . . . . . 6-4 Index I-1
ii
Chapter
1
Introduction
Chapter1
What is C++Builder?
C++Builder is an object-oriented, visual programming environment for rapid
application development (RAD). Using C++Builder, you can create highly efficient
32-bit Windows applications with a minimum of manual coding. C++Builder
provides all the tools you need to develop, test, debug, and deploy applications,
including a large library of reusable components, a suite of design tools, application
and form templates, and programming wizards. These tools simplify prototyping
and shorten development time.
Introduction 1-1
Where to find information
Online Help
The online Help system provides detailed information about user-interface features,
language implementation, programming tasks, and the components in the Visual
Component Library (VCL). It includes the core Help files listed in Table 1.1
You will also find Help on additional products that are supplied with some versions
of C++Builder, such as:
• Integrated Translation Environment (ITE) Help
• InterBase Express Reference Help
• Borland Database Engine (BDE) Help
• BDE Administrator Help
• Database Explorer Help
• Local SQL, SQL Builder, and SQL Monitor Help
• TeamSource Help
• CodeGuard Help
• TurboAssembler Help
• Package Collection Editor Help
• Help Author’s Guide (Help Workshop)
• QuickReport Help
• TeeChart Help
• InterBase and InterBase Express Help
• CORBA Component Library Reference Help
• Help for miscellaneous components (FastNet Time, DayTime, Echo, Finger, HTTP,
NNTP, POP3, Powersock, SMTP, UDP, URL Encode/Decode, UUprocessor,
Stream and Msg components)
All Help files are located in the Help directory under the main C++Builder directory.
Introduction 1-3
Typographic conventions
For information on customizing your Help system, see “Customizing the Help
system” on page 5-9.
Printed documentation
This Quick Start is an introduction to C++Builder. To order additional printed
documentation, refer to the Web site at shop.borland.com.
Typographic conventions
This manual uses the typefaces described below to indicate special text.
Palette of ready-
made components
to use in your
applications.
C++Builder’s development model is based on two-way tools. This means that you can
move back and forth between visual design tools and text-based editing. For
example, after using the Form Designer to arrange buttons and other elements in a
graphical interface, you can immediately view the form (.DFM) file that contains the
textual description of your form. You can also manually edit any code generated by
C++Builder without losing access to the visual programming environment.
From the IDE, all your programming tools are within easy reach. You can manage
projects, design graphical interfaces, write code, search databases, compile, test,
debug, and browse through class libraries without leaving the IDE.
To learn about organizing and configuring the IDE, see Chapter 5, “Customizing the
environment”.
List of projects Trace To find out what a button does, point to it for a moment
you can run into and a hint is displayed. The button’s keyboard shortcut, if
it has one, is displayed as well.
Or choose a
component from an
alphabetical list.
Select a property
and change its
value in the right
column.
Many properties have simple values—such as names of colors, true or false, and
integers. For Boolean properties, you can double-click the word to toggle between
true and false. Some properties have associated property editors to set more complex
values. When you click on such a property value, you’ll see an ellipsis.
Double-click here to
change the value from
true to false.
Or double-click in the
value column, and
C++Builder generates
skeleton code for a new
event handler.
Components added
to the form are
reflected in the code.
Generated
code.
Exploring code
When a source file is open in the Code editor, you can use the ClassExplorer to see a
structured table of contents for the code. The ClassExplorer contains a tree diagram
showing the types, classes, properties, methods, global variables, and routines
defined in your unit.
Managing projects
Use the Project Manager to organize the form and unit files that make up an
application. To display the Project Manager, choose View|Project Manager.
The Project Manager shows you the form, unit, resource, object, library, and other
files contained in a project. You can use the Project Manager to add and remove files,
and you can open any file by double-clicking it.
You can combine related projects into a single project group. For example, you might
use project groups to organize a multi-tiered application or to keep DLLs with
executables that use them.
The Data Module Designer makes it easy to create data modules. To create a data
module, choose File|New and double-click on Data Module.
C++Builder opens an empty data module in the Data Module Designer, displays the
unit file for the new module in the Code editor, and adds the module to the current
project. When you reopen an existing data module, C++Builder displays its
components in the Data Module Designer.
Getting help
The online Help system provides extensive documentation on the VCL and other
parts of C++Builder. Here are some of the ways you can display Help:
Press F1 on a property
or event name in the
Object Inspector to
display VCL Help.
Press F1 on a language
keyword or VCL element in
the Code editor.
Press F1 on an object in
the Form Designer.
You can get Help on any part of the development environment, including menu
items, dialog boxes, windows, toolbars, and components.
Pressing the Help button in any dialog box also displays context-sensitive online
documentation.
Error messages from the compiler and linker appear in a special window below the
Code editor. To get Help with compilation errors, select a message from the list and
press F1.
To configure these tools, choose Tools|Editor Options and click the Code Insight tab.
Debugging applications
The IDE includes an integrated debugger that helps you locate and fix errors in your
code. The debugger lets you control program execution, watch variables, and modify
data values while your application is running. You can step through your code line
by line, examining the state of the program at each breakpoint.
Run button
To use the debugger, you must compile your program with debug information.
Choose Project|Options, select the Compiler page, and check Debug Information.
Then you can begin a debugging session by running the program from the IDE. To
set debugger options, choose Tools|Debugger Options.
Many debugging windows are available, including Breakpoints, Call Stack, Watches,
Local Variables, Threads, Modules, CPU, and Event Log. Display them by choosing
Once you set up your desktop as you like it for debugging, you can save the settings
as the debugging or runtime desktop. This desktop layout will be used whenever
you are debugging any application. For details, see “Customizing desktop settings”
on page 5-5.
Some versions of C++Builder support multiprocess and remote debugging of
distributed applications from either the client or the server. To turn on remote
debugging, choose Run|Parameters, click the Remote tab, and check “Debug Project
on remote machine”.
Exploring databases
The SQL Explorer (or Database Explorer in some editions of C++Builder) lets you
work directly with a remote database server during application development. For
example, you can create, delete, or restructure tables, and you can import constraints
while you are developing a database application.
Choose Database|Explore
to display the Explorer.
You can see and change
the data in a table.
And you can query a
database directly.
You can add your own objects to the Repository to facilitate reusing them and
sharing them with other developers. Reusing objects lets you build families of
applications with common user interfaces and functionality; building on an existing
foundation also reduces development time and improves quality. The Object
Repository provides a central location for tools that members of a development team
can access over a network.
To add objects to the Repository, right-click in the New Items dialog and choose
Properties, or choose Tools|Repository from the main menu.
3 Choose File|Save All to save your files to disk. When the Save dialog appears:
• Navigate to your TextEditor folder.
• Save Unit1 using the default name Unit1.pas.
• Save the project using the name TextEditor.bpr. (The executable will be named
the same as the project name with an exe extension.)
Later, you can resave your work by choosing File|Save All.
When you save your project, C++Builder creates additional files in your project
directory. These files include TextEditor.bpr, which is the project options or
makefile, and TextEditor.res, which is the Windows resource file. You don’t need
to worry about these files but don’t delete them.
When you open a new project, C++Builder displays the project’s main form, named
Form1 by default. You’ll create the user interface and other parts of your application
by placing components on this form.
Next to the form, you’ll see the Object Inspector, which you can use to set property
values for the form and components you place on it.
To start designing the text editor, add a RichEdit and a StatusBar component to the
form:
1 To create a text area, drop a RichEdit component onto the form.
Click the Win32 page on the Component palette. To find the RichEdit component,
point to an icon on the palette for a moment; C++Builder displays a Help hint
showing the name of the component.
When you find the RichEdit component, double-click it to place it on the form.
The RichEdit component now fills the form so you have a large text editing area.
By choosing the alClient value for the Align property, the size of the RichEdit
control will vary to fill whatever size window is displayed even if the form is
resized.
3 Double-click the StatusBar component on the Win32 page of the Component
palette. This adds a status bar to the bottom of the form.
Next we want to create a place to display the name of the file being edited. You
can do this in two ways. The easiest way is to set the SimplePanel property of the
StatusBar1 object to true and assign any text that you want to display to the
SimpleText property. This provides only one panel in the status bar. You can assign
its value as follows:
StatusBar1->SimpleText = "untitled.txt";
However, many times you will want to include more than one panel in the status
bar so you can include more than one piece of information. You can specify more
than one panel by using the Panels property of the TStatusBar component as
explained in the next few steps.
4 Double-click the status bar to display the Editing StatusBar1->Panels dialog box.
5 Right-click on the dialog box and choose Add to add the panel to the status bar.
The Panels property is a zero-based array that allows you to access each Panel that
you create based on its unique index value (by default, it is 0 for this panel). Use
the default property values for the panel. Click the X in the upper right corner to
close the dialog box. Now the main editing area of the user interface for the text
editor is set up.
Following are the kinds of actions our sample text editor application needs:
You can also centralize images to use for your toolbar and menus in an ImageList.
To add an ActionList and an ImageList to your form:
1 From the Standard page of the Component palette, drop an ActionList component
onto the form. The ActionList component is nonvisual, so it doesn’t matter where
you put it on the form. It won’t appear at runtime.
2 From the Win32 page, choose the ImageList component and drop it onto your form.
It’s also nonvisual so you can put it anywhere.
Your form should now resemble the following figure.
The ActionList
and ImageList
objects don’t
show when the
application is
running.
Editing area
Status bar
3 In the Object Inspector, set the following properties for the action:
• After Caption, type &New. Note that typing an ampersand before one of the letters
makes that letter a shortcut to accessing the command.
• After Category, type File. This organizes the File commands in one place.
• After Hint, type Create file (this will be the Help hint).
• After ImageIndex, type 0 (this will associate image number 0 in your ImageList
with this action).
• After Name, type FileNew (for the File|New command).
4 Right-click on the Action List editor and choose New Action.
5 In the Object Inspector, set the following properties:
• After Caption, type &Open.
• Make sure the Category says File.
• After Hint, type Open file.
• After ImageIndex, type 1.
• After Name, type FileOpen (for the File|Open command).
Note When you were adding actions to the action list, you might have noticed a standard
Help|Contents command is provided. We added a custom Help|Contents
command that will display the Help Contents tab at all times. The standard Help|
Contents command brings up the last tabbed page that was displayed, either the
Contents or the Index.
2 Double-click TEditCut. The action is created along with a new category called
Edit. EditCut1 should be selected.
3 In the Object Inspector, set the following property for EditCut1:
• After ImageIndex, type 4.
The other properties are set automatically.
4 Right-click on the Action List editor and choose New Standard Action.
5 Double-click TEditCopy.
6 In the Object Inspector, set the following properties:
• After ImageIndex, type 5.
7 Right-click on the Action List editor and choose New Standard Action.
8 Double-click TEditPaste.
9 In the Object Inspector, set the following properties:
• After ImageIndex, type 6.
10 Now you’ve got all the actions that you’ll need for the menus and toolbar. If you
click on the category All Actions, you can see all the actions in the list:
11 Click on the X to close the Action List editor.
12 With the Action List still selected on the form, set its Images property to
ImageList1.
Click on the Images property, then on the down arrow next to Images.
ImageList1 is listed for you. Select it. This associates the images that we’ll add
to the image list with the actions in the action list.
When you’re done close the Action List editor. Now you’re ready to add the menu
and toolbar.
Adding a menu
In this section, you’ll add a main menu bar with three drop-down menus—File, Edit,
and Help—and you’ll add menu items to each one using the actions in the action list.
1 From the Standard page of the Component palette, drop a MainMenu component
onto the form. It doesn’t matter where you place it.
2 Set the main menu’s Images property to ImageList1. This will allow you to add the
images to the menu items.
3 Double-click the menu component to display the Menu Designer.
4 In the Object Inspector, type &File to set the Caption property of the first top-level
menu item and press Enter.
5 In the Menu Designer, select the File item you just created. You’ll notice an empty
item under it: select the empty item. In the Object Inspector, choose the Action
property. The Actions from the action list are all listed there. Select FileNew.
• Focus on the item under New and choose FileOpen from its Action property.
• Focus on the item under Open and choose FileSave from its Action property.
• Focus on the item under Save and choose FileSaveAs from its Action property.
• Focus on the item under Save As and type a hyphen as the Caption of the next
item under the File menu and press Enter to create a separator bar on the menu.
• Focus on the item under the separator bar and choose FileExit from its Action
property.
6 Next create the Edit menu:
• Point to the item to the right of the File command and set its Caption property
to &Edit and press Enter.
• Focus is now on the item under Edit; choose EditCut1 from its Action property.
• Select the item under Cut and choose EditCopy1 from its Action property.
• Select the item under Copy and choose EditPaste1 from its Action property.
• Focus on the Menu Designer to select the item under Help and choose
HelpContents from its Action property.
• Select the item under Contents and choose HelpIndex from its Action property.
• Select the item under Index and type a hyphen its Caption and press Enter to
create a separator bar on the Help menu.
• Select the item under the separator bar and choose HelpAbout from its Action
property.
8 Click on the X to close the Menu Designer.
9 Choose File|Save to save your project.
10 Press F9 to compile and run the project. (You can also run the project by clicking
the Run button on the Debug toolbar, or by choosing Run from the Run menu.)
When you run your project, C++Builder opens the program in a window like the
one you designed on the form. The program is a full-fledged Windows
application, complete with Minimize, Maximize, and Close buttons and a Control
menu. The menus all work although most of the commands are grayed out. The
images are displayed next to menu items with which we associated icons.
Though your program already has a great deal of functionality, there’s still more
to do to activate the commands. And we want to add a toolbar to provide easy
access to the commands.
11 Click the X in the upper right corner to close the application and return to the
design-time view of the form.
3 Select the text you want to remove in the String List editor, press the Delete key,
and click OK.
4 Save your changes and trying running the program again.
The text editing area is now cleared when the main form is displayed.
Adding a toolbar
Since we’ve set up actions in an action list, we can add some of the same actions that
were used on the menus onto a toolbar.
1 On the Win32 page of the Component palette, double-click the ToolBar to add it to
the form.
A blank toolbar is added under the main menu. With the toolbar still selected,
change the following properties in the Object Inspector:
• Set the toolbar’s Indent property to 4. (This indents the icons 4 pixels from the
left of the toolbar.)
• Set its Images property to ImageList1.
• Set ShowHint to true. (Tip: Double-click on false to change it to true.)
2 Add buttons and separators to the toolbar:
• With the toolbar selected, right-click and choose New Button four times.
• Right-click and choose New Separator.
• Right-click and choose New Button three more times.
• Right-click and choose New Separator.
• Right-click and choose New Button once again.
Note Don’t worry if the icons aren’t correct yet. The correct icons will be selected when
you assign actions to the buttons.
3 Assign actions from the action list to the first set of buttons.
• Select the first button and set its Action to FileExit.
• Select the second button and set its Action to FileNew.
• Select the third button and set its Action to FileOpen.
• Select the fourth button and set its Action to FileSave.
4 Assign actions to the second set of buttons.
• Select the first button and set its Action to EditCut1.
• Select the second button and set its Action to EditCopy1.
• Select the third button and set its Action to EditPaste1.
5 Assign an action to the last button.
• Select the last button and set its Action to HelpContents.
6 Press F9 to compile and run the project.
Your text editor already has lots of functionality. You can type in the text area.
Check out the toolbar. If you select text in the text area, the Cut, Copy, and Paste
buttons work.
7 Click the X in the upper right corner to close the application and return to the
design-time view.
6 Right where the cursor is positioned in the text editor (between { and }), type the
following lines:
RichEdit1->Clear();
FileName = "Untitled.txt";
StatusBar1->Panels->Items[0]->Text = FileName;
Your event handler should look like this when you’re done:
• Double-click the text area next to Filter to display the Filter editor. Specify filters
for file types: Type “Text files” as the Filter Name and *.txt as the filter and “All
files” as a second Filter Name and *.* as its filter). Then click OK.
4 The Action List editor should still be displayed. If it’s not, double-click the
ActionList icon on the form.
5 In the Action List editor, double-click the FileOpen action.
The Code editor opens with the cursor inside the event handler.
6 Right where the cursor is positioned in the text editor (between { and }), type the
following lines:
if (OpenDialog1->Execute())
{
RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
FileName = OpenDialog1->FileName;
StatusBar1->Panels->Items[0]->Text = FileName;
}
Your FileOpen event handler should look like this when you’re done:
Your event handler should look like this when you’re done:
Note The Action List editor should still be displayed. If it’s not, double-click the
ActionList icon on the form.
3 In the Action List editor, double-click the FileSaveAs action.
The Code editor opens with the cursor inside the event handler.
4 Right where the cursor is positioned in the text editor, type the following lines:
SaveDialog1->FileName = FileName;
SaveDialog1->InitialDir = ExtractFilePath(FileName);
if (SaveDialog1->Execute())
{
RichEdit1->Lines->SaveToFile(SaveDialog1->FileName);
FileName = SaveDialog1->FileName;
StatusBar1->Panels->Items[0]->Text = FileName;
}
Your FileSaveAs event handler should look like this when you’re done:
To see what it looks like so far, run the application by pressing F9 or by clicking on
the green Run button on the toolbar.
Most of the buttons and toolbar buttons work but we’re not finished yet.
To return to design mode, close the Text Editor application by choosing File|Exit, by
clicking the Exit application button on the toolbar of your application, or by clicking
the X in the upper right corner.
If you receive any error messages, click on them to locate the error. Make sure you’ve
followed the steps as described in the tutorial.
Tip To get Help on the HelpCommand method, put the cursor next to HelpCommand
in the editor and press F1.
That’s it for the Help|Contents command.
3 Select the following TLabel items in the About box and change them in the Object
Inspector:
• Change Product Name to Text Editor.
• Make it Version 1.0.
• Enter the year next to Copyright.
4 Select the form itself and change its Caption in the Object Inspector to About Text
Editor.
Tip The easiest way to select the form is to click on the grid portion.
5 Save the About box form by choosing File|Save As and saving it as About.cpp.
6 In the C++Builder editor, you should have three files displayed: Unit1.cpp,
Unit1.h, and About.cpp. Click on the Unit1.cpp tab.
7 Add an include statement for the About unit to Unit1. Choose File|Include Unit
Hdr and then select About and click OK.
Notice that #include About.h has been added to the top of the .cpp file.
Click on the tab to display a file associated with a unit. If you open
other files while working on a project, additional tabs appear on the
editor.
When you create a new form for your application, you need to add it to the
main form. Choose File|Include Unit Hdr and select the header to add.
8 On the action list, double-click the HelpAbout action to create an event handler.
9 Right where the cursor is positioned in the text editor, type the following line:
AboutBox->ShowModal();
This code opens the About box when the user clicks Help|About. ShowModal
opens the form in a modal state. That means the user can’t do anything until the
form is closed.
3 In the Events tab, double-click OnCreate to create an event handler that describes
what happens when the form is created (that is, when you open the application).
4 Right where the cursor is positioned in the text editor, type the following lines:
Application->HelpFile = ExtractFilePath(Application->ExeName) + "TextEditor.hlp";
FileName = "Untitled.txt";
StatusBar1->Panels->Items[0]->Text = FileName;
RichEdit1->Clear();
This code initializes the application by associating a Help file, setting the value of
FileName to untitled.txt, putting the filename into the status bar, and clearing out
the text editing area.
5 Put the .HLP file and the CNT file into the project application directory (called
projects\TextEditor).
Note If you decided not to investigate how to create a Help file or use the sample one
provided on the web, the application still works but you’ll receive an error
message when you choose either of the Help commands or click Help on the
toolbar.
6 Press F9 to run the application.
You can test the Text Editor now to make sure it works. If errors occur, click on the
error message and you’ll go right to the place in the code where the error occurred.
Congratulations! You’re done.
When you find the Table component, click it once to select it, then click on the form
to place the component. The Table component is nonvisual, so it doesn’t matter
where you put it. C++Builder names the object Table1 by default. (When you point
4 Set the AutoHint property of the status bar to true. The easiest way to do this is to
double-click on false next to AutoHint in the Object Inspector. (Setting AutoHint to
true allows Help hints to appear in the status bar at runtime.)
Connecting to a database
The next step is to add database controls and a DataSource to your form.
1 From the Data Access page of the Component palette, drop a DataSource
component onto the form. The DataSource component is nonvisual, so it doesn’t
matter where you put it on the form. Set its DataSet property to Table1.
2 From the Data Controls page, choose the DBGrid component and drop it onto your
form. Position it in the lower left corner of the form above the status bar, then
expand it by dragging its upper right corner.
If necessary, you can enlarge the form by dragging its lower right corner. Your
form should now resemble the following figure.
3 Set DBGrid properties to align the grid with the form. Double-click Anchors in the
Object Inspector to display akLeft, akTop, akRight, and akBottom; set them all to true.
4 Set the DataSource property of DBGrid to DataSource1 (the default name of the
DataSource component you just added to the form).
Now you can finish setting up the Table1 object you placed on the form earlier.
5 Select the Table1 object on the form, then set its TableName property to BIOLIFE.DB.
(Name is still Table1.) Next, set the Active property to true.
When you set Active to true, the grid fills with data from the BIOLIFE.DB database
table. If the grid doesn’t display data, make sure you’ve correctly set the properties
of all the objects on the form, as explained in the instructions above.
The DBGrid control displays data at design time, while you are working in the
IDE. This allows you to verify that you’ve connected to the database correctly. You
cannot, however, edit the data at design time; to edit the data in the table, you’ll
have to run the application.
6 Press F9 to compile and run the project. (You can also run the project by clicking
the Run button on the Debug toolbar, or by choosing Run from the Run menu.)
In connecting your application to a database, you’ve used three components and
several levels of indirection. A data-aware control (in this case, a DBGrid) points to a
DataSource object, which in turn points to a dataset object (in this case, a Table).
Finally, the dataset (Table1) points to an actual database table (BIOLIFE), which is
accessed through the BDE alias BCDEMOS. (BDE aliases are configured through the
BDE Administrator.)
data-aware control dataset
(Grid) DataSource (Table) BDE database
This architecture may seem complicated at first, but in the long run it simplifies
development and maintenance. For more information, see “Designing database
applications” in the Developer’s Guide or online Help.
Right-click in the
editor and choose
New Standard Action
to display the
Standard Actions
list box.
Select the actions you
want and click OK.
Press Ctrl to select
multiple actions.
6 Right-click on the Action List editor and choose New Standard Action. The
Standard Actions list box is displayed.
7 Select the following actions: TDataSetFirst, TDataSetLast, TDataSetNext,
TDataSetPrior, TEditCopy, TEditCut, and TEditPaste. (Use the Ctrl key to select
multiple items.) Then click OK.
Adding a menu
In this section, you’ll add a main menu bar with three drop-down menus—File, Edit,
and Record—and you’ll add menu items to each one using the standard actions in
the action list.
1 From the Standard page of the Component palette, drop a MainMenu component
onto the form. It doesn’t matter where you place it.
2 Set the main menu’s Images property to ImageList1.
4 Type &File to set the Caption property of the first top-level menu item and press
Enter.
5 Type &Save and press Enter to create a Save menu item under File.
6 Type a hyphen in the next item under the File menu and press Enter to create a
separator bar on the menu.
7 Type E&xit and press Enter to create an Exit menu item under File.
8 Click on the second top-level menu item (to the right of File), type &Edit, and press
Enter. The first menu item under Edit is selected.
• In the Object Inspector, set its Action to EditCut1 and press Enter. The item’s
caption is automatically set to Cut.
• Select the next menu item (under Cut) and set its Action to EditCopy1.
• Select the next menu item and set its Action to EditPaste1.
9 Click on the third top-level menu item (to the right of Edit), type &Record as its
caption, and press Enter. The menu item under Record is selected.
• In the Object Inspector, set its Action to DataSetFirst1.
• Select the next menu item and set its Action to DataSetPrior1.
• Select the next menu item and set its Action to DataSetNext1.
• Select the next menu item and set its Action to DataSetLast1.
Adding a toolbar
1 On the Win32 page of the Component palette, double-click the ToolBar to add it to
the form.
• Set the toolbar’s Indent property to 4.
• Set its Images property to ImageList1.
• Set ShowHint to True.
2 Add buttons to the toolbar.
• With the toolbar selected, right-click and choose New Button three times.
• Right-click and choose New Separator.
• Right-click and choose New Button four more times.
3 Assign actions to the first set of buttons.
• Select the first button and set its Action to EditCut1.
• Select the second button and set its Action to EditCopy1.
• Select the third button and set its Action to EditPaste1.
4 Assign actions to the second set of buttons.
• Select the first button and set its Action to DataSetFirst1.
• Select the second button and set its Action to DataSetPrior1.
• Select the third button and set its Action to DataSetNext1.
• Select the last button and set its Action to DataSetLast1.
Displaying images
Each record in the BIOLIFE database has a picture associated with it. In this section,
you’ll expand your application to display pictures.
1 From the Standard page of the Component palette, drop a Panel component onto
the form below the toolbar. C++Builder names this Panel1 by default.
2 In the Object Inspector, delete the Panel1 string from the panel’s Caption property.
Leave the Caption property blank.
3 Align Panel1 to the top of the form by setting its Align property to alTop. Next, drag
the bottom of the panel down so it fills the portion of the form between the toolbar
and the grid.
6 Set the DataSource property of DBImage to DataSource1. Then set its DataField
property to Graphic. (In the Object Inspector, the drop-down list next to DataField
shows the fields in the BIOLIFE table. Graphic is one of the field names.)
As soon as you set DataField to Graphic, the DBImage component displays the
image of a fish corresponding to the first record of the table. This shows that you
have correctly hooked up to the database.
6 Customize the Font property of the DBText component using the Font editor.
The Font editor is a property editor that you can access through the Object
Inspector. Select the Font property in the Object Inspector; an ellipsis button
appears on the right side of the property setting. Click the ellipsis button to
display the Font editor.
Modify the following DBText settings using the Font editor:
• Set the Font Style to Bold.
• Set the Color to Silver.
• Set the Size to 12.
Then click OK.
7 Compile and run your application by pressing F9.
You can view and edit the data in the DBMemo component. The DBText component
displays data for reading only.
Close the application when you’re ready to continue.
double-click certain objects on a form, C++Builder creates a skeleton handler for the
default event.
You can also access all of a component’s events through the Object Inspector. Select
an object on a form, then click the Events tab on the Object Inspector; you’ll see a list
of the object’s events. To create a skeleton handler for any event, double-click in the
space to its right.
For more information about events and event handlers, see “Developing the
application user interface” in the Developer’s Guide or online Help.
To write an event handler:
1 From the Dialogs page of the Component palette, drop a SaveDialog component
onto the form. This is a nonvisual component, so it doesn’t matter where you place
it. C++Builder names it SaveDialog1 by default. (When SaveDialog‘s Execute method
is called, it invokes a standard Windows dialog for saving files.)
2 From the menu on your form, choose File|Save. C++Builder creates a skeleton
event handler for the event that occurs at runtime when the user selects Save from
the File menu. The Code editor opens with the cursor inside the event handler.
This event handler is attached to the OnClick event of the main menu’s first menu
item. The menu item is an instance of the class TMenuItem, and OnClick is its
default event. Hence the Save1Click method is a default event handler.
3 Complete the event handler by adding the code shown below between the
outermost pair of braces.
void __fastcall TForm1::Save1Click(Tobject *Sender)
{
std::FILE *outfile;
AnsiString Buffer;
Buffer.sprintf("Save Info For: %s", DBText1->Field->AsString.c_str());
SaveDialog1->Title = Buffer;
if (SaveDialog1->Execute())
{
outfile = std::fopen(SaveDialog1->FileName.c_str(), "wt");
if (outfile)
{
fprintf(outfile, "Facts on the %s\n\n",
DBText1->Field->AsString.c_str());
for (int i=0; i < DBGrid1->FieldCount; i++)
fprintf(outfile, "%s: %s\n",
DBGrid1->Fields[i]->FieldName.c_str(),
DBGrid1->Fields[i]->AsString.c_str());
fprintf(outfile, "\n%s\n", DBMemo1->Text.c_str());
}
fclose(outfile);
}
}
This event handler calls the Execute method in the SaveDialog component. When
the dialog box opens and the user specifies a file name, it saves fields from the
current database record into a file.
This code uses file I/O from the standard library. Before compiling it, add:
#include <cstdio>
to the top of the unit file just beneath the line that reads:
#include <vcl.h>
4 To add code for the Exit command, choose File|Exit. C++Builder generates
another skeleton event handler and displays it in the editor.
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
}
Right where the cursor is positioned (between the braces), type:
Close();
5 Choose File|Save All to save your work. Then press F9 to run the application.
You can exit the program using the now functional File|Exit command.
While debugging, you can dock Watch and Breakpoint windows onto the Code
editor.
To dock a window, drag it over another window until the first window’s rectangular
outline becomes narrow and vertical; then release the mouse. To undock a window,
click its title bar and drag it in any direction.
You can move toolbars and menus within the main window. Click the grabber (the double bar on the
left) and drag it to where you want it.
Main window
organized
differently.
You can even separate parts from the main window and place them elsewhere on the
screen or remove them from the desktop altogether.
Choose View|Toolbars|
Customize.
From the Commands
page, select any
command and drag it
onto the toolbar.
In the same way that you specify a default project, you can specify a default main
form and a default new form. The default main form is the form created when you
begin a new application. The default new form is the form created when you choose
File|New Form to add a form to an open project. If you haven’t specified a default
form, C++Builder uses a blank form.
You always have the option to override your default project or forms by choosing
File|New and selecting from the New Items dialog box.
Installing components
You can supplement the components in the VCL with custom components that you
write yourself or obtain from third-party developers. To make new components
available at design time, you need to install them in the IDE.
OpenHelp lets you add any WinHelp files to C++Builder‘s Help system, including
documentation for third-party products. OpenHelp also allows you to remove
references to obsolete Help files from the system registry.
For an overview of the Help files supplied with C++Builder, see “Online Help” on
page 1-2.
The diagram below shows some of the principal classes that make up the VCL.
TObject
TGraphicControl TWinControl
TScrollingWinControl TCustomControl
Exception handling
C++Builder’s error-handling is based on exceptions, which are special objects
generated in response to unanticipated input or faulty program execution.
Exceptions can be thrown at both design time and runtime, and the VCL contains
many exception classes that are associated with specific error conditions. In your
applications, you’ll want to write exception handlers to deal gracefully with runtime
errors. Exceptions can also be a valuable debugging tool, since the class of an
exception often provides a clue about what caused it to be thrown.
BDE Administrator
Use the BDE Administrator (BDEAdmin.exe) to configure BDE drivers and set up the
aliases used by data-aware VCL controls to connect to databases.
Database Desktop
The Database Desktop (DBD32.exe) lets you create, view, and edit Paradox and
dBase database tables in a variety of formats.
Data Dictionary
The Data Dictionary provides a customizable storage area, independent of your
applications, where you can create extended field attribute sets that describe the
content and appearance of data. The Data Dictionary can reside on a remote server
for additional sharing of information.
DLLs
Dynamic-link libraries (DLLs) are compiled modules containing routines that can be
called by applications and by other DLLs. Since a DLL contains sharable code or
resources, it is typically used by more than one application.
Frames
A frame (TFrame), like a form, is a container for other components. In some ways, a
frame is more like a customized component than a form. Frames can be saved on the
Component palette for easy reuse, and they can be nested within forms, other frames,
or other container objects. After a frame is created and saved, it continues to function
as a unit and to inherit changes from the components (including other frames) it
contains. When a frame is embedded in another frame or form, it continues to inherit
changes made to the frame from which it derives.
Type libraries
Type libraries are files that include information about data types, interfaces, member
functions, and object classes exposed by an ActiveX control or server. By including a
type library with your COM application or ActiveX library, you make information
about these entities available to other applications and programming tools.
C++Builder provides a Type Library editor for creating and maintaining type
libraries.
Deploying applications
When you deploy an application, be sure to supply all the required files—including
executables, DLLs, packages, and BDE drivers—to your users. To make this process
easier, C++Builder includes a special version of InstallShield Express, a popular tool
for developing installation utilities.
Internationalizing applications
C++Builder offers many features for internationalizing and localizing applications.
Support for input method editors (IMEs) and extended character sets is provided
throughout the VCL, and tools like the Resource DLL wizard make it easy to prepare
a project for localization. To get the maximum benefit from these features, you need
to start thinking about internationalization requirements as early as possible in the
development process.
The Integrated Translation Environment (ITE), available in some versions of
C++Builder, is a suite of tools for software localization and simultaneous
development for different locales. It is integrated with the IDE to let you manage
multiple localized versions of an application as part of a single project.
The ITE includes three tools:
• Translation Manager, a grid for viewing and editing translated resources
• Translation Repository, a sharable database for translations
• Resource DLL wizard, a DLL wizard that generates and manage resource DLLs
B D
BDE (Borland Database Engine) 6-3 Data Dictionary 6-4
Administrator 4-6, 6-3 Data Module Designer 2-8 to 2-9, 6-3
aliases 4-6, 6-3 data modules 2-8 to 2-9
.BPR files 3-2 data-aware controls 4-6, 6-3
buttons (VCL) 6-2 Database Desktop 6-4
Database Explorer 2-13, 6-3
C databases 2-13 to 2-14
accessing 3-5, 4-2 to 4-6, 6-3
character sets, extended 6-6 architecture of database connection 4-6
charts 6-1 controls 4-2, 4-4
classes 3-4, 4-3 report generation 6-1
ClassExplorer 2-7, 5-7 tools and utilities 6-3
Code Completion 2-11 dataset components 4-6
Code editor 2-5 to 2-12 DataSource component 4-4, 4-6
browsing 2-6 DB2 6-3
options 5-6 dBASE 6-3
Code Insight 2-11 DBGrid component 4-4
Code Parameters 2-11 DBImage component 4-11
Code Templates 2-11 DBMemo component 4-12
color DBText component 4-12
Code editor 5-6 DCOM 1-2
forms 4-2 debugging 2-12 to 2-13
COM 1-2, 6-1, 6-4, 6-5 arranging views and tool windows 5-2
compiling 4-6 remote 2-13
Component palette 2-3, 3-3, 4-2, 6-2 decision support 6-1
customizing 5-7 default event handlers 4-14
component templates 5-8 default events 4-13, 4-14
components 2-3, 3-2, 3-3, 4-2 default forms 5-5
arranging on the Component palette 5-8 default project options 2-9, 5-5
customizing 1-2, 5-8, 6-5 default projects 5-5
documentation 1-2 deploying applications 6-6
installing 5-8, 6-5 design time 3-3
setting properties 2-4, 3-3 design-time settings 3-3
templates 5-8 desktop settings 5-5
third-party 5-8, 6-5 developer support 1-4
VCL hierarchy 6-2
Index I-1
.DFM files 2-6, 3-1 image list 3-12
docking tool windows 5-1 ImageList component 3-7, 4-6
documentation 1-1 to 1-4, 2-10 to 2-11 images 3-12
ordering 1-4 displaying 4-10
dynamic-link libraries (DLLs) 6-4 IMEs 6-6
indentation, Code editor 5-6
E Informix 6-3
input method editors 6-6
editor example 3-1 to 3-29 installation utilities 6-6
Environment Options dialog 2-9, 5-6 InstallShield Express 6-6
errors instance objects 3-4, 4-3
compiler 2-11 integrated development environment (IDE)
exception handling 6-2 2-1 to 2-2, 6-1
event handlers 2-5, 3-17 to 3-24, 4-13 to 4-15 customizing 5-1 to 5-9
default 4-14 Integrated Translation Environment (ITE) 6-6
events 2-5, 3-17, 4-13 to 4-15, 6-2 InterBase 6-3
default 4-13, 4-14 InterBase Express (IBX) 6-3
Events page (Object Inspector) 2-5 internationalization 6-6
example program 3-1 to 3-29, 4-1 to 4-15
exceptions 6-2 K
F keyboard shortcuts 2-2
keystroke mappings 5-6
Filter editor 3-20
Font editor 4-13 L
fonts, Code editor 5-6
Form Designer options 5-6 libraries, C and C++ 1-2, 4-15
form files 2-6, 3-1 localization 6-6
forms 2-3, 3-2
default 5-5 M
new 5-5
FoxPro 6-3 MainMenu component 3-13, 4-7
frames 6-5 makefiles 3-2
marine life example 4-1 to 4-15
G menus 2-2, 3-13
configuring 5-4
graphics, displaying 4-10 context 2-2
grid snap 5-6 messages
grids (database) 4-4 error 3-29
GUIs, creating 2-3, 3-2 windows 6-2
Microsoft Transaction Server (MTS) 6-1
H modal state 3-28
MTS 1-2
header files 3-1 multithreading 6-1
Help files, creating 3-24 multi-tiered applications 6-1
Help system
accessing 2-10 to 2-11 N
context-sensitive 2-10 to 2-11
customizing 1-3, 5-9 new features 1-1
files 1-2 New Items dialog (File|New) 2-14, 5-6
highlighting, syntax 5-6 newsgroups 1-4
.HLP files 1-2, 5-9
HTTP 1-2 O
I Object Inspector 2-4 to 2-5, 3-2
overview 2-4
icons 3-12 Object Pascal 1-3
Index I-3
I-4 Quick Start