UNITS CHANGEOWNER 代码如下:
program CHANGEOWNER;uses
Forms,
YUANLAI in 'YUANLAI.pas' {Form1},
HOULAI in 'HOULAI.pas' {Form2};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end.UNITS YUANLAI代码如下:
unit YUANLAI;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ButtonCHG: TButton;
Button1: TButton;
ButtonLIST: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure ButtonLISTClick(Sender: TObject);
procedure ButtonCHGClick(Sender: TObject);
private
{ Public declarations }
public
{ Public declarations }
end; var
Form1: TForm1;implementationUSES HOULAI ;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('My owner is '+((Sender as TButton).Owner as TForm).Name);
end;procedure TForm1.ButtonLISTClick(Sender: TObject);
var i : Integer;
begin
self.ListBox1.Items.Clear;
for i:= 0 to Componentcount-1do
begin
self.ListBox1.Items.Add(Components[i].Name);
end;
end;procedure change(com ,newowner: TComponent);
begin
com.Owner.RemoveComponent(com);
newowner.InsertComponent(com);
end;procedure TForm1.ButtonCHGClick(Sender: TObject);
begin
if Assigned(self.Button1)
then
self.Button1.Parent := Form2;
change(Button1, Form2);
end;
end.UNINTS HOULAI 代码如下:
unit HOULAI;interfaceuses
Windows, Messages, SysUtils, Variants, 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
self.ListBox1.Items.Clear;
for i:= 0 to Componentcount-1do
begin
self.ListBox1.Items.Add(Components[i].Name);
end;
end;end.
谢谢 ,谢谢!
program CHANGEOWNER;uses
Forms,
YUANLAI in 'YUANLAI.pas' {Form1},
HOULAI in 'HOULAI.pas' {Form2};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end.UNITS YUANLAI代码如下:
unit YUANLAI;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ButtonCHG: TButton;
Button1: TButton;
ButtonLIST: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure ButtonLISTClick(Sender: TObject);
procedure ButtonCHGClick(Sender: TObject);
private
{ Public declarations }
public
{ Public declarations }
end; var
Form1: TForm1;implementationUSES HOULAI ;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('My owner is '+((Sender as TButton).Owner as TForm).Name);
end;procedure TForm1.ButtonLISTClick(Sender: TObject);
var i : Integer;
begin
self.ListBox1.Items.Clear;
for i:= 0 to Componentcount-1do
begin
self.ListBox1.Items.Add(Components[i].Name);
end;
end;procedure change(com ,newowner: TComponent);
begin
com.Owner.RemoveComponent(com);
newowner.InsertComponent(com);
end;procedure TForm1.ButtonCHGClick(Sender: TObject);
begin
if Assigned(self.Button1)
then
self.Button1.Parent := Form2;
change(Button1, Form2);
end;
end.UNINTS HOULAI 代码如下:
unit HOULAI;interfaceuses
Windows, Messages, SysUtils, Variants, 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
self.ListBox1.Items.Clear;
for i:= 0 to Componentcount-1do
begin
self.ListBox1.Items.Add(Components[i].Name);
end;
end;end.
谢谢 ,谢谢!
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Form2.show;
Application.Run;
end.