Delphi
VCL Form Basics
Create a form with event handlers in Delphi VCL.
By EZ4Code Team
vclformevents
Code
unit MainFormUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, Vcl.Forms, Vcl.StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FClickCount: Integer;
public
property ClickCount: Integer read FClickCount;
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
FClickCount := 0;
Caption := 'My Delphi App';
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
Inc(FClickCount);
Button1.Caption := 'Clicked ' + IntToStr(FClickCount) + ' times';
end;
end.Explanation
VCL forms are .pas (code) + .dfm (designer) pairs. {$R *.dfm} links the form resources. Event handlers use Sender (the component that fired). Common events: OnClick, OnCreate, OnShow, OnClose. Components are dropped on the form at design time; properties set in Object Inspector.
More Delphi Snippets
Units and Classes
Define units with interface and implementation sections.
Properties and Events
Define properties and event handlers in Delphi.
Generics
Type-safe containers with generics in Delphi.
Interfaces and Reference Counting
Define interfaces with automatic reference counting.
Exception Handling
Try/Except/Finally in Delphi.
RTTI (Runtime Type Information)
Inspect types and properties at runtime.