在循环里动态创建控件,form上可以看到2个panel,分别为panel0和panel1.但是只有panel0上有label, panel1上确没有,不知道为什么 求解,谢谢大家。
procedure TForm1.Button2Click(Sender: TObject);
var
  i:integer;
begin
    for i := 0 to 1 do
  begin
    newPanel1(i);
  end;
end;procedure TForm1.newPanel1(i: integer);
var
  mypanel :TPanel;
  label2 : TLabel;
begin
  mypanel := tpanel.Create(self);
  mypanel.Parent := form1;
  mypanel.Width:=300;
  mypanel.Height:=300;
  mypanel.Left:=i*(mypanel.Width + 20)+5;
  mypanel.Top:=10;
  mypanel.Caption := 'panel'+inttostr(i);  label2 := tLabel.Create(mypanel);
  label2.Parent := mypanel;
  label2.Top:=mypanel.Top + 10;
  label2.Width:=100;
  label2.AutoSize:=false;
  label2.Left:=mypanel.Left+10;
  label2.Caption:='点点滴滴'+inttostr(i);
  showmessage(inttostr(label2.Parent.Handle));
end;

解决方案 »

  1.   

    你的Label组件的父窗口是Panel, 那么Label组件的坐标是相对Panel的左上角为原点的,Label的Left就不需要加Panel的Left了。label2.Top:=mypanel.Top + 10;
    label2.Left:=mypanel.Left+10;
    改成:
    label2.Top := 10;
    label2.Left := 10;
    即可。
      

  2.   

    诶呀吗呀,妖哥啊。
    在2003年的时候您老人家就指导我C++ BUILDER,记忆犹新啊。
    好久没用Delphi和c++ builder了,都忘了
    在膜拜一下妖哥我去试试代码。
      

  3.   

    好用了,太激动了,遇到妖哥比代码问题解决了还激动。妖哥您老人家现在总混CSDN吗?
      

  4.   


    procedure TForm1.newPanel1(i: integer);
    var
      mypanel :TPanel;
      label2 : TLabel;
    begin
      mypanel := tpanel.Create(self);
      mypanel.Parent := form1;
      mypanel.Width:=300;
      mypanel.Height:=300;
      mypanel.Left:=i*(mypanel.Width + 20)+5;
      mypanel.Top:=10;
      mypanel.Caption := 'panel'+inttostr(i); 
      label2 := tLabel.Create(mypanel);
      label2.Parent := mypanel;
      label2.Top:=10;
      label2.Width:=100;
      label2.AutoSize:=false;
      label2.Left:=10;
      label2.Caption:='点点滴滴'+inttostr(i);
      showmessage(inttostr(label2.Parent.Handle));
    end;