A VCL Application

Our next example is an application built using the Visual Component Library (VCL) components in the IDE. This program uses automatically generated form and resource files, so you won't be able to compile it from the source code alone. But it illustrates important features of the Delphi Language. In addition to multiple units, the program uses classes and objects

The program includes a project file and two new unit files. First, the project file:

Once again, our program is called greeting. It uses three units: Forms, which is part of VCL; Unitl, which is associated with the application's main form (Forml); and Unit2, which is associated with another form (Form2).

The program makes a series of calls to an object named Application, which is an instance of the TApplication class defined in the Forms unit. (Every project has an automatically generated Application object.) Two of these calls invoke a TApplication method named CreateForm. The first call to CreateForm creates Formi, an instance of the TForml class defined in Uniti. The second call to CreateForm creates Form2, an instance of the TForm2 class defined in Unit2.

Uniti looks like this:

Uniti creates a class named TFormi (derived from TForm) and an instance of this class, Formi. TFormi includes a buttonButtoni, an instance of TButton and a procedure named Buttoniciick that is called when the user presses Buttoni. Buttoniciick hides Formi and it displays Form2 (the call to Form2.ShowModal).

Note: In the previous example, Form2.showModal relies on the use of auto-created forms. While this is fine for example code, using auto-created forms is actively discouraged. Form2 is defined in Unit2:

unit Unit2; interface uses SysUtils, Types, Classes, Graphics, Controls, Forms, Dialogs; type

TForm2 = class(TForm) Labeli: TLabel; CancelButton: TButton;

procedure CancelButtonClick(Sender: TObject); end;

Form2: TForm2; implementation uses Uniti; {$R *.dfm}

procedure TForm2.CancelButtonClick(Sender: TObject); begin

Form2.Close; end;

end.

Unit2 creates a class named TForm2 and an instance of this class, Form2. TForm2 includes a button (CancelButton, an instance of TButton) and a label (Labeli, an instance of TLabel). You can't see this from the source code, but Label1 displays a caption that reads Hello world! The caption is defined in Form2's form file, Unit2.dfm.

TForm2 declares and defines a method CancelButtonClick which will be invoked at runtime whenever the user presses CancelButton. This procedure (along with Unit1's TForm1.Button1Click) is called an event handler because it responds to events that occur while the program is running. Event handlers are assigned to specific events by the form files for Form1 and

Form2.

When the greeting program starts, Form1 is displayed and Form2 is invisible. (By default, only the first form created in the project file is visible at runtime. This is called the project's main form.) When the user presses the button on Form1, Form2, displays the Hello world! greeting. When the user presses the CancelButton or the Close button on the title bar, Form2 closes.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment