它的安全清算组件?

时间:2016-11-15 12:46:59

标签: delphi delphi-10.1-berlin

我使用此代码清空填写表单:

var
i: integer;

for i:=0 to componentcounts-1 do
    begin
    if component[i] is TEdit then
       (component[i] as Tedit).text:='';
   .....another component also include
    end;

但我更喜欢在表单之外使用此代码,以便可以使用其他表单

然后我创建一个程序

procedure emptyForm(f:Tform)
var
   i:integer;
begin
with f do
     begin
     for i:=0 to componentcounts-1 do
         begin
         if component[i] is TEdit then
           (component[i] as Tedit).text:='';
           //.....another component also include
        end;
     end;
end;

它的保存是这样做的吗?

1 个答案:

答案 0 :(得分:-1)

我认为没关系,但使用'with'有点危险。要了解原因,请记住TForm是TComponent的后代,并且具有许多与Component [i]相同的属性,从而导致潜在的混淆和错误。我更喜欢以下

procedure emptyForm(f:Tform);
var
   i:integer;
   iComponent : TComponent;
begin
     for i:=0 to f.componentcount-1 do
     begin
         iComponent := f.Components[ I ];
         if iComponent is TEdit then
         begin
           (iComponent as Tedit).text:='';
         end;
           //.....another component also include
     end;
end;