可以用数组实现创建label,但是label放在form的不同地方,这个问题怎么解决?请大家贴点代码,谢谢各位了!有答案就给分,不够可以加!

解决方案 »

  1.   

    Label1 := TLabel.Create(nil);
    Label1.Parent := Panel1;
    Label1.Name := 'Label1';
    Label1.Left := 100;  //就这两句
    Label1.Top := 100;  //就这两句
      

  2.   

    我是动态生成的,Form上要有8个label动态生成,而且每个label的top和left都不相同
      

  3.   

    var
      Label1:TLabel;
    begin
      Label1 := TLabel.Create(nil); 
      Label1.Parent := Panel1; 
      Label1.Name := 'Label1'; 
      Label1.Left := 100;  //就这两句 
      Label1.Top := 100;  //就这两句
    end;可以吧??
      

  4.   

    你可以根据创建Label的先后顺序,Top与Left成比例赋值。这样就是不同的位置,具体要看规律!
      

  5.   

    procedure TForm1.FormCreate(Sender: TObject);
    var
     i:integer;
     bttn:array[1..8] of TLabel ;
    begin
     for   i:=1   to  8  do
      begin
          Bttn[i]:=TLabel.Create(Self);
          bttn[i].Parent:=self;
          bttn[i].AutoSize:=false;
          Bttn[i].Tag:=i;
          Bttn[i].Left:=100;
          bttn[i].Top:=30*i;
          bttn[i].Caption:='标签'+inttostr(i);
          Bttn[i].OnClick:=btnClick;  //定义单击事件。
          Bttn[i].OnMouseMove:=FrmMouseMove;//定义移动事件。
      end;
    end;
      

  6.   

          Bttn[i].OnClick:=btnClick;  //定义单击事件。 
          Bttn[i].OnMouseMove:=FrmMouseMove;//定义移动事件。去掉上面这两句。
      

  7.   

    谢谢各位了,尤其感谢mwy654321,问题已经基本解决了,还有一个问题是,动态创建的label怎么响应不同onclick事件?
      

  8.   

    如果你的位置是开始就知道的那好做呀,上面的方法就可以了如果你的位置不是确定的那么可以这样marayLabel:array[0..7] of TLable;procedure CreateLabel;
    var
      intI:integer;
    begin
      for intI:=Low(marayLabel) to High(marayLabel) do
      begin
        marayLabel[intI]:=TLabel.create(application);
        marayLabel[intI].Visible:=false;
        marayLabel[intI].parent:=from;  
      end;
    end;procedure ShowLabel(intLeft:integer; intTop:integer; strCaption:string; intIndex:integer);
    begin
      marayLabel[intIndex].caption:=strCaption;
      marayLabel[intIndex].Left:=intLeft;
      marayLabel[intIndex].Top:=intTop;
      marayLabel[intIndex].visible:=true;
    end;//这样应该可以了,呵呵 
      

  9.   

    如果你的位置是开始就知道的那好做呀,上面的方法就可以了如果你的位置不是确定的那么可以这样marayLabel:array[0..7] of TLable;procedure CreateLabel;
    var
      intI:integer;
    begin
      for intI:=Low(marayLabel) to High(marayLabel) do
      begin
        marayLabel[intI]:=TLabel.create(application);
        marayLabel[intI].Visible:=false;
        marayLabel[intI].parent:=from;  
      end;
    end;procedure ShowLabel(intLeft:integer; intTop:integer; strCaption:string; intIndex:integer);
    begin
      marayLabel[intIndex].caption:=strCaption;
      marayLabel[intIndex].Left:=intLeft;
      marayLabel[intIndex].Top:=intTop;
      marayLabel[intIndex].visible:=true;
    end;//这样应该可以了,呵呵 
      

  10.   

    “qkhhxkj102”的方法我看还是不错的,好像也可以用到label的onclick事件中,多谢了!
      

  11.   

    根据tag属性判断就可以相应不同的onclick了
      

  12.   

    既然如此,结贴时多给我点分就OK了。下面是你要的代码:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure btnClick(Sender:TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure   TForm1.btnClick(Sender:TObject);
    begin
     ShowMessage(Format('No.%d  被单击啦!',[(Sender   as   TLabel).Tag]));
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
     i:integer;
     bttn:array[1..8] of TLabel ;
    begin
     for   i:=1   to  8  do
      begin
          Bttn[i]:=TLabel.Create(Self);
          bttn[i].Parent:=self;
          bttn[i].AutoSize:=false;
          Bttn[i].Tag:=i;
          Bttn[i].Left:=100;
          bttn[i].Top:=30*i;
          bttn[i].Caption:='标签'+inttostr(i);
          Bttn[i].OnClick:=btnClick;  //定义单击事件。
      end;
    end;end.