unit Unit1; uses //... type Form1: TForm1; implementation procedure TForm1.FormCreate(Sender: TObject); begin Form2 := TFrom2.create(self); end; procedure TFrom1.Button2Click(Sender: TObject); begin Form1.caption := 'Hi'; //不要再TForm1类中国实用Form1 end; unit Unit2; var Form2: TForm2; implementation //...
unit Unit1; uses //... type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private FForm:TForm; public property Form: TType read FForm write FForm; end; var Form1: TForm1; implementation procedure TForm1.FormCreate(Sender: TObject); begin Form2 := TFrom2.create(self); end; procedure TFrom1.Button1Click(Sender: TObject); begin if Assigned(FForm) then TForm2(FForm).Shoe;//访问的是内部成员FForm,主要FForm需要转型. end; procedure TFrom1.Button2Click(Sender: TObject); begin self.caption := 'Hi';//在TForm1类中实用self代替Form1. end; //一下是项目文件中的内容 program Project1; uses Forms, unit1 in 'Unit1.pas'{Form1}, Unit2 in 'Unit2.pas'{Form2}; begin Application.Initialize; Application.CreateForm(TForm1,Form1); Application.CreateForm(TForm2,Form2); //通过属性传递Form2的引用 Form1.FForm := Form2; Application.Run; end;
请发表评论