我在unit2中建了一个过程,是清空edit框中的内容,想在unit1中也引用,能清空unit1 中的edit框中的内容,但是引用后清空的是unit2中的edit框中的内容,并没有清空unit1中的内容。
代码如下:
unit2代码:
......
type
 TForm2 = class(TForm)
   Edit1: TEdit;
   Edit2: TEdit;
   Button1: TButton;
   procedure ClearEdit; //自定义过程
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;
var
 Form2: TForm2;implementation{$R *.dfm}procedure tform2.ClearEdit;  //实现
Var
i:Integer;
begin
  for i:=0 to self.componentcount-1 do
begin
 if self.components[i] is Tedit then
   (components[i] as tedit).text:='';
end;
end;
.......
unit1代码:
....implementation
 uses unit2;
{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
begin
form2.ClearEdit;              //在form1中引用,但并没有清空form1中的edit
end;  

解决方案 »

  1.   

    self对象是FORM2,即使你不使用SELF作为前缀同样也是FORM2的数据被清空,你应该将TFORM或TFORMCLASS作为参数传递到过程中对FORM实例的组件进行处理即可。

    procedure ClearEdit(Form:TComponent);
    var
      i:integer;
    begin
      if assigned(form) then
      for i:=0 to form.ComponentCount-1 do
       if Form.Components[i] is TEdit then
        (form.Components[i] as TEdit).text='';
    //.....
        
    end;  
      

  2.   

    (unit2)public
        procedure clearEdit(vClass: TForm; count: integer);
    procedure TForm2.clearEdit(vClass: TForm; Count: integer);
    var
      i: integer;
    begin
      for i:=0 to count do
      begin
        if (vClass.Components[i] is TEdit) then
          (vClass.Components[i] as TEdit).Clear;
      end;
    end;
    (unit1)  Form2.clearEdit(Form1,self.ComponentCount-1);
    面向对象的书你要好好看看