把btn:TButton;放到public区定义,你放在当前过程中只是个局部变量,无法引用。

解决方案 »

  1.   

    你好像不能在这个位置设置“name”
    你不需要对:btn.name付值,当然你的btn要在public或private都可以(看你的自己的了)
    直接用btn就可以了,btn.free也是必不可少的,哈哈。
      

  2.   

    self.findcomponent('btnNew') as Tbutton
      

  3.   

    不对啊,看看这个例子
    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 do begin
        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;
    用FindComponent可以实现对在form上动态创建控件的引用
    我的控件是创建到gropebox上的,应该也这样引用
    正在尝试:)
      

  4.   

    groupbox上不要用self self表示from1,你可以用groupbox代替这样创建的tedit就会在groupbox中
      

  5.   

    lbl:=GroupBox3.FindComponent('lblActive0') as Tlabel;
    怎么在groupbox上还是找不到
    cry~~~~~~~~
      

  6.   

    lbl:=self.FindComponent('lblActive0') as Tlabel;
    终于找到了:)
    看来GroupBox不是容器
      

  7.   

    我用FlatGroupbox竟然不是容器,我倒~~~~~~~~
    看来第三方控件用起来要小心
      

  8.   

    可以设置控件数组:var btn:array [1..20] of TSpeedButton;
    begin
     for i:= 1 to 20 do
     begin
      btn[i] := TSpeedButton.Create(groupbox1);
      btn[i].Parent := groupbox1;
     end;
     ...
     for i:= 1 to 20 do
      btn[i].free;