用 with 可以减少重复的输入。

解决方案 »

  1.   

    The following example creates 20 edit boxes, using FindComponent with the edit box name to access each newly created edit box.procedure TForm1.Button1Click(Sender: TObject);var
      i: Integer;
    const
      NamePrefix = 'MyEdit';
    begin
      for i := 1 to 20 dobegin
        TEdit.Create(Self).Name := NamePrefix + IntToStr(i);
        with TEdit(FindComponent(NamePrefix + IntToStr(i))) do
        begin
          Left := 10;
          Top := i * 20;
          Parent := self;
        end;
      end;
    end;
      

  2.   

    with VarForm do
    begin
         button1.caption:=...
         button2.caption:=...
         ...
    end;
      

  3.   

    我的思路接近linx88,但不是受他的影响。我给出对此题的直接答案:假设创建这些Form时,Application是它们的Owner。如用Application.CreateForm,或TXXXXForm.Create( Application )的方式创建。AForm := Application.FindComponent( Format('sj_%s_w', [Tablename]) ); // 先根据Table的名字确定Form的名字,然后找到Form对象(TForm是一个TComponent)
    TEdit(AForm.FindComponent('edit1')).Text := 'dasdas'; // 用控件名找到控件对象,然后转换类型,并对其属性赋值
    TButton(AForm.FindComponent('button1')).Caption := 'dasdas';
    这里假定所有的FindComponent都成功。