Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 6

Chapter 04 - Project IfSender

Project Structure

IfSender.dpr
program IfSender;

uses
  Forms,
  SendForm in 'SendForm.pas' {SenderForm};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TSenderForm, SenderForm);
  Application.Run;
end.
SendForm.pas
unit SendForm;

interface

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

type
  TSenderForm = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    BitBtn1: TBitBtn;
    Button2: TButton;
    CheckBox1: TCheckBox;
    Edit1: TEdit;
    procedure ShowSender(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  SenderForm: TSenderForm;

implementation

{$R *.DFM}

procedure TSenderForm.ShowSender(Sender: TObject);
begin
  Memo1.Lines.Add ('Class Name: ' +
    Sender.ClassName);

  if Sender.ClassParent <> nil then
    Memo1.Lines.Add ('Parent Class: ' +
      Sender.ClassParent.ClassName);

  Memo1.Lines.Add ('Instance Size: ' +
    IntToStr (Sender.InstanceSize));

  // if the object is (exactly) of the TButton type
  if Sender.ClassType = TButton then
    Memo1.Lines.Add ('TButton ClassType');

  // if the object is a given object
  if Sender = Button1 then
    Memo1.Lines.Add ('This is Button1');

  if Sender.InheritsFrom (TButton) then
    Memo1.Lines.Add ('Sender inherits from TButton');

  if Sender is TButton then
    Memo1.Lines.Add ('Sender is a TButton');

  // leave a blank line
  Memo1.Lines.Add ('');
end;

end.
SendForm.dfm
object SenderForm: TSenderForm
  Left = 229
  Top = 192
  Width = 383
  Height = 361
  Caption = 'SenderForm'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 16
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = ShowSender
  end
  object Memo1: TMemo
    Left = 104
    Top = 16
    Width = 257
    Height = 305
    TabOrder = 1
  end
  object BitBtn1: TBitBtn
    Left = 16
    Top = 80
    Width = 75
    Height = 25
    Caption = 'BitBtn1'
    TabOrder = 2
    OnClick = ShowSender
  end
  object Button2: TButton
    Left = 16
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Button2'
    TabOrder = 3
    OnClick = ShowSender
  end
  object CheckBox1: TCheckBox
    Left = 16
    Top = 112
    Width = 73
    Height = 17
    Caption = 'CheckBox1'
    TabOrder = 4
    OnClick = ShowSender
  end
  object Edit1: TEdit
    Left = 16
    Top = 136
    Width = 73
    Height = 21
    TabOrder = 5
    Text = 'Edit1'
    OnClick = ShowSender
  end
end