Quick start manual
Overview
2-7
Example programs
Unit1 creates a class named TForm1 (derived from TForm) and an instance of this
class, Form1. TForm1 includes a button—Button1, an instance of TButton—and a
procedure named TForm1.Button1Click that is called at runtime whenever the user
presses Button1. TForm1.Button1Click hides Form1 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 {different units would be used for a Windows-specific program}
SysUtils, Types, Classes, QGraphics, QControls, QForms, QDialogs;
type
TForm2 = class(TForm)
Label1: TLabel;
CancelButton: TButton;
procedure CancelButtonClick(Sender: TObject);
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.xfm}
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 (Label1, 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.xfm.
TForm2 declares and defines a method CancelButtonClick which will be invoked at
runtime whenever the user presses CancelButton; it closes Form2. This procedure
(along with Unit1’s TForm1.Button1Click) is known as 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.