Logo Delphi Handbooks Collection

Delphi Developer Days 2012
March-May
Cantù-Jensen
(UK, NL, US, D, I)

Menu for Development

Site Menu
Delphi 2010 Handbook
Delphi 2009 Handbook
Delphi 2007 Handbook
Mastering Borland Delphi 2005
Essential Pascal
Essential Delphi
Buy Books Online
Code Repository
Newsgroups
White Papers
Tools
Conferences
Training
Delphi Links
Contact Marco

My Other Sites
Italian Site (www.marcocantu.it)
Developers Newsgroups Browser (dev.newswhat.com)
My town (www.piazzacavalli.net)
the delphi search
Wintech Italia (my company)

Advertising
Home My Blog Handbooks Development Links Marco
Delphi Developer Days 2012


Home: Code Repository: Mastering Delphi 6

Chapter 05 - Project ChangeOwner

Project Structure

ChangeOwner.dpr
program ChangeOwner;

uses
  Forms,
  ChOwn1 in 'ChOwn1.pas' {Form1},
  ChOwn2 in 'ChOwn2.pas' {Form2};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
ChOwn1.pas
unit ChOwn1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    ButtonChange: TButton;
    ButtonList: TButton;
    ListBox1: TListBox;
    procedure ButtonChangeClick(Sender: TObject);
    procedure ButtonListClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses ChOwn2;

{$R *.DFM}

procedure ChangeOwner (Component, NewOwner: TComponent);
begin
  Component.Owner.RemoveComponent (Component);
  NewOwner.InsertComponent (Component);
end;

procedure TForm1.ButtonChangeClick(Sender: TObject);
begin
  if Assigned (Button1) then
  begin
    // change parent
    Button1.Parent := Form2;
    // change owner
    ChangeOwner (Button1, Form2);
  end;
end;

procedure TForm1.ButtonListClick(Sender: TObject);
var
 I: Integer;
begin
  ListBox1.Items.Clear;
  for I := 0 to ComponentCount - 1 do
    ListBox1.Items.Add (Components[I].Name);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage ('My owner is ' +
    ((Sender as TButton).Owner as TForm).Caption);
end;

end.
ChOwn2.pas
unit ChOwn2;

interface

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

type
  TForm2 = class(TForm)
    ButtonList: TButton;
    ListBox1: TListBox;
    procedure ButtonListClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}


procedure TForm2.ButtonListClick(Sender: TObject);
var
 I: Integer;
begin
  ListBox1.Items.Clear;
  for I := 0 to ComponentCount - 1 do
    ListBox1.Items.Add (Components[I].Name);
end;

end.
ChOwn1.dfm
object Form1: TForm1
  Left = 194
  Top = 201
  Width = 482
  Height = 176
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 152
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ButtonChange: TButton
    Left = 56
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Change'
    TabOrder = 1
    OnClick = ButtonChangeClick
  end
  object ButtonList: TButton
    Left = 312
    Top = 16
    Width = 75
    Height = 25
    Caption = 'List'
    TabOrder = 2
    OnClick = ButtonListClick
  end
  object ListBox1: TListBox
    Left = 280
    Top = 48
    Width = 145
    Height = 89
    ItemHeight = 13
    TabOrder = 3
  end
end
ChOwn2.dfm
object Form2: TForm2
  Left = 205
  Top = 284
  Width = 483
  Height = 176
  Caption = 'Form2'
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  Visible = True
  PixelsPerInch = 96
  TextHeight = 13
  object ButtonList: TButton
    Left = 320
    Top = 16
    Width = 75
    Height = 25
    Caption = 'List'
    TabOrder = 0
    OnClick = ButtonListClick
  end
  object ListBox1: TListBox
    Left = 280
    Top = 48
    Width = 145
    Height = 89
    ItemHeight = 13
    TabOrder = 1
  end
end