我在程序中动态生成了一些控件,但不知怎么标识动态生成的这些控件,给Name属性赋值类型没用对,看了帮助也不是太懂,有没有高手帮忙解决一下

解决方案 »

  1.   

    Create;
    //其他属性
    ....
    Free;
      

  2.   

    我不知楼上具体是什么意思,比如动态创建一个Button
    var ButtonTest:TButton;
    begin
      ButtonTest:=TButton.Create(Self);
      ButtonTest.Name:='abc';
     
      //我在这一步想给他一个标识,以便在程序中控制它,但错误提示类
        不对,帮助说是TComponentName类型的,但对这个类型的解释却是如下:
        Type TComponentName=Type Strings;
       不知你明不明白问题怎么回事  
    end;
      

  3.   

    刚才表述可能有误会,是Name的类型不对,提示显示应该是TComponentName类型的
      

  4.   

    var ButtonTest:TButton;
    begin
      ButtonTest:=TButton.Create(Self);
      ButtonTest.Name:='abc';
      ButtonTest.Free;
    end;在DELPHI 5 里能编译通过!
      

  5.   

    楼上说的我是过,在Delphi6里编译也能通过,但一执行就出现异常,不知你试过没有
      

  6.   

    用hint属性表示这个button,然后用的时候遍历所有的控件,然后查看他们的hint 属性,如果符合你刚才的命名方式,那么就是他。
      

  7.   

    to M16():
    你说的好像能实现,但我觉得效率会比较低,因为我可能会在一个窗口放很多控件
      

  8.   

    如果你是想动态创建一个按钮,然后都用同一个消息响应事件,同时要区分发送消息是的哪一个按钮,是吧,我用的是tag属性来标识的,如:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)    //动态按钮的响应事件
        Procedure buttononclick(Sender:TObject);    procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.buttononclick(Sender: TObject);
    begin
      case TButton(Sender).Tag of
      1: showmessage('This is Test1');
      2: showmessage('This is Test2');
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      ButtonTest:TButton;
      btntop,
      btnleft:Integer;begin
      btntop:=10;
      btnleft:=10;
      ButtonTest:=TButton.Create(Self);
      With ButtonTest do
      begin
        tag:=1;
        Caption:='test1';
        parent:= self;
        Top:=btntop;
        Left:=btnleft;
        onclick := buttononclick;
      end;  btntop:=100;
      btnleft:=100;
      ButtonTest:=TButton.Create(Self);
      With ButtonTest do
      begin
        tag:=2;
        Caption:='test2';
        parent:= self;
        Top:=btntop;
        Left:=btnleft;
        onclick := buttononclick;
      end;
    end;end.  
      

  9.   

    //==============================================================================
    //动态创建控件******************************************************************
    //==============================================================================
    function DynaCreateComponent(Owner: TComponent; CompType: TControlClass; CompName: String; Left,Top,Width,Height:Integer): TControl;
    begin
      if (Owner.FindComponent(CompName)<>nil) and not(Owner.FindComponent(CompName) is TControl) then
      begin
        Result := nil;
        exit;
      end;
      Result := Owner.FindComponent(CompName) as TControl;
      if Result=nil then
      begin
        Result := CompType.Create(Owner);
        with Result do
        begin
          if Owner is TwinControl then
          begin
            SetBounds(Left,Top,Width,Height);
            Parent := TwinControl(Owner);{如果是可视构件,则显示之}
            if Owner is TForm then TForm(Owner).ActiveControl := TWinControl(Result);{设置窗口焦点}
          end;
        end;
        Result.Name := CompName;
      end
      else {Result<>Nil}
      if not(Result is CompType) then
      begin
        Result := nil;
        Exit;
      end;
      Result.Visible := True;
    end;
    { 对于未知数量的控件组,利用TList
      var ControlList: Tlist; CreateNum: integer;
      const CreateClass : TControlClass = TButton;//可以任意修改TControlClass = TEdit或TPanel等。效果一样。
      var i:integer; APoint: Pointer;
      ControlList := TList.Create;
      ControlList.Clear;
      CreateNum := 10;
      for i:=1 to CreateNum do
          begin
            APoint := Pointer(DynaCreateComponent(self,CreateClass,'Button_' + IntToStr(i),0,i*20+1,60,20));//创建
            ControlList.Add(APoint);
          end;
      TButton(ControlList.Items[i]).Caption := 'XXXX';}
      

  10.   

    控件动态生成以后,名字就已经定下来,不能更改,你最好用其他属性来标识。
    比如caption来储存string名称,tag来储存integer名称