Marco's Web Center

Menu for Development

Site Menu
Object Pascal Handbook
Delphi Handbooks Collection
Mastering Borland Delphi 2005
(Old) White Papers
(Old)Tools
(Old) Conferences

My Other Sites
Italian Site (www.marcocantu.it)
the delphi search

Spirit of delphi

Advertising
Home My Blog Books Object Pascal Marco

Home: Code Repository: Mastering Delphi 6

Chapter 03 - Project Animals1

Project Structure

Animals1.dpr
program Animals1;

uses
  Forms,
  AnimF in 'AnimF.pas' {FormAnimals},
  Anim in 'Anim.pas';

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TFormAnimals, FormAnimals);
  Application.Run;
end.
AnimF.pas
unit AnimF;

interface

uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Anim;

type
  TFormAnimals = class(TForm)
    LabelKind: TLabel;
    BtnKind: TButton;
    RadioAnimal: TRadioButton;
    RadioDog: TRadioButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnKindClick(Sender: TObject);
    procedure RadioAnimalClick(Sender: TObject);
    procedure RadioDogClick(Sender: TObject);
  private
    MyAnimal: TAnimal;
  end;

var
  FormAnimals: TFormAnimals;

implementation

{$R *.DFM}

procedure TFormAnimals.FormCreate(Sender: TObject);
begin
  MyAnimal := TAnimal.Create;
end;

procedure TFormAnimals.FormDestroy(Sender: TObject);
begin
  MyAnimal.Free;
end;

procedure TFormAnimals.BtnKindClick(Sender: TObject);
begin
  LabelKind.Caption := MyAnimal.GetKind;
end;

procedure TFormAnimals.RadioAnimalClick(Sender: TObject);
begin
  MyAnimal.Free;
  MyAnimal := TAnimal.Create;
end;

procedure TFormAnimals.RadioDogClick(Sender: TObject);
begin
  MyAnimal.Free;
  MyAnimal := TDog.Create;
end;

end.
Anim.pas
unit Anim;

interface

type
  TAnimal = class
  public
    constructor Create;
    function GetKind: string;
  private
    Kind: string;
  end;

  TDog = class (TAnimal)
  public
    constructor Create;
  end;

implementation

constructor TAnimal.Create;
begin
  Kind := 'An animal';
end;

function TAnimal.GetKind: string;
begin
  GetKind := Kind;
end;

constructor TDog.Create;
begin
  Kind := 'A dog';
end;

end.
AnimF.dfm
object FormAnimals: TFormAnimals
  Left = 213
  Top = 114
  Width = 287
  Height = 196
  ActiveControl = BtnKind
  Caption = 'Animals'
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object LabelKind: TLabel
    Left = 39
    Top = 120
    Width = 201
    Height = 25
    Alignment = taCenter
    AutoSize = False
    Caption = 'Kind'
    Font.Charset = ANSI_CHARSET
    Font.Color = clBlack
    Font.Height = -16
    Font.Name = 'Arial'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object BtnKind: TButton
    Left = 160
    Top = 40
    Width = 81
    Height = 25
    Caption = '&Kind'
    TabOrder = 0
    OnClick = BtnKindClick
  end
  object RadioAnimal: TRadioButton
    Left = 24
    Top = 24
    Width = 105
    Height = 33
    Caption = '&Animal'
    Checked = True
    TabOrder = 1
    TabStop = True
    OnClick = RadioAnimalClick
  end
  object RadioDog: TRadioButton
    Left = 24
    Top = 56
    Width = 113
    Height = 41
    Caption = '&Dog'
    TabOrder = 2
    OnClick = RadioDogClick
  end
end