The quickest way to introduce yourself to Delphi is to write an application. This tutorial guides you through the creation of a program that navigates a marine-life database. After setting up access to the database, you'll write an event handler that opens a standard Save As dialog box, allowing you to write information from the database to a file.
Before beginning a new application, create a folder to hold the source files.
Each application is represented by a project . When you start Delphi, it opens a blank project by default. If another project is already open, choose File|New Application to create a new project.
When you open a new project, Delphi automatically creates the following files.
Each form has its own unit and form files.
Later on, you can save your work at any time by choosing File|Save All.
When you save your project, Delphi creates additional files in your project directory. You don't need to worry about them but don't delete them.
When you open a new project, Delphi 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.
When you use the Object Inspector to set properties, Delphi maintains your source code for you. The values you set in the Object Inspector are called design-time settings.
Find the form's Color property in the Object Inspector and click the drop-down list displayed to the right of the property. Choose clAqua from the list.
The Component palette represents components by icons grouped onto tabbed pages. Add a component to a form by selecting the component on the palette, then clicking on the form where you want to place it. You can also double-click a component to place it in the middle of the form.
Add a Table and a StatusBar to the form:
Click the Data Access tab on the Component palette. To find the Table component, point at an icon on the palette for a moment; Delphi displays a Help hint showing the name of the component.
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. Delphi names the object Table1 by default. (When you point to the component on the form, Delphi displays its name--Table1--and the type of object it is--TTable.)
Each Delphi component is a class; placing a component on a form creates an instance of that class. Once the component is on the form, Delphi generates the code necessary to construct an instance object when your application is running.
Select Table1 on the form, then choose the DatabaseName property in the Object Inspector. Select DBDEMOS from the drop-down list.
The next step is to add database controls and a DataSource to your form.
If necessary, you can enlarge the form by dragging its lower right corner. Your form should now resemble the following figure.
Now you can finish setting up the Table1 object you placed on the form earlier.
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. (Also verify that you copied the sample database files into your ...\Borland Shared\Data directory when you installed Delphi.)
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.
In connecting our application to a database, we'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 DBDEMOS. (BDE aliases are configured through the BDE Administrator.)
This architecture may seem complicated at first, but in the long run it simplifies development and maintenance. For more information, see "Developing database applications" in the Developer's Guide or online Help.
When you run your project, Delphi 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. You can scroll through the BIOLIFE data in the grid.
Though your program already has a great deal of functionality, it still lacks many features usually found in Windows applications. For example, most Windows applications implement menus and toolbars to make them easy to use.
In this section, you'll prepare your application for additional graphical-interface elements by setting up an ActionList component. While you can create menus, toolbars, and buttons without using action lists, action lists simplify development and maintenance by centralizing responses to user commands.
You've added standard actions. Now you're ready to add the menu and toolbar.
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.
&File to set the Caption property of the first top-level menu item and press Enter.&Save and press Enter to create a Save menu item under File.
E&xit and press Enter to create an Exit menu item under File.
&Edit, and press Enter. The first menu item under Edit is selected.
&Record as its caption, and press Enter. The menu item under Record is selected.
Press F9 to run your program and see how it looks.
Close the application when you're ready to continue.
Check out the toolbar. The First, Prior, Next, and Last buttons work. Select text within a cell in the grid; the Cut, Copy, and Paste buttons work as well.
Close the application when you're ready to continue.
Each record in the BIOLIFE database has a picture associated with it. In this section, we'll expand our application to display pictures.
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.
Close the application when you're ready to continue.
In this section, you'll add two components that display individual text fields from the database.
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, then click OK.
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.
Up to this point, you've developed your application without writing a single line of code. By using the Object Inspector to set property values at design time, you've taken full advantage of Delphi's RAD environment. In this section, however, you'll write procedures called event handlers that respond to user input while the application is running. You'll connect the event handlers to menu items, so that when a menu item is selected your application executes the code in the 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.
procedure TForm1.Save1Click(Sender: TObject);
var
i: integer;
begin
SaveDialog1.Title := Format('Save info for %s', [DBText1.Field.AsString]);
if SaveDialog1.Execute then
begin
with TStringList.Create do
try
Add(Format('Facts on the %s', [DBText1.Field.AsString]));
Add(#13#10);
for i := 1 to DBGrid1.FieldCount-3 do
Add(Format('%s : %s',
[DBGrid1.Fields[i].FieldName,
DBGrid1.Fields[i].AsString]));
Add(Format(#13#10+'%s'+#13#10,[DBMemo1.Text]));
SaveToFile(SaveDialog1.FileName);
finally
Free;
end;
end;
end;
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.
procedure TForm1.Exit1Click(Sender: TObject); begin end;
Right where the cursor is positioned (between begin and end), type
Close;
You can exit the program using the now functional File|Exit command.
Most components on the Component palette have events, and most components have a default event. A common default event is OnClick, which gets called whenever the component is clicked; for example, if you placed a Button component (TButton) on a form, you would almost certainly write an OnClick event handler for it. When you double-click certain objects on a form, Delphi 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.
pubsweb@borland.com
Copyright © 1999, Inprise Corporation. All rights reserved.