大家好.我在写程序时遇到这样一个问题:
在一个Panel中放了Image控件.Image控件将根据性别贴上不同的图片.Panel的Name,Caption属性也将在程序运行时根据条件而改变.
请问:
如何在程序运行时根据指定数目 复制/生成 上述 Panel ?
谢谢!

解决方案 »

  1.   

    没装delphi没测试,大致是这样var 
        Image:   TImage; 
    begin 
        for i:=1 to n do
      begin
        Image:=TImage.Create(Self);
        Image.Parent:=Form1;
        Image.Name:='button'+inttostr(i);
        Image.Left:=40*i;
        Image.Top:=20;
        Image.Text:='button'+inttostr(i);
        Image.width:=50;
      end;end; 
      

  2.   

    没大看明白楼主的意思,我的想法是这样的:首先跟据指定的数目动态生成Panel
    然后取得已经存在的Panel的属性,对新建的Panel进行赋值
      

  3.   

    感觉不是复制panle ,而是自动生成panelwhile not self.adoquery1.eof do
    begin
      i:=self.adoquery1.recno;
      xb:=self.adoquery1.fieldbyname('xb);//性别
      name:=self.adoquery1.fieldbyname('name');//姓名
      pa:=Tpanel.create(form1);
      pa.parent:=form1;
      pa.align:=altop;
      pa.name:='panel'+inttostr(i);
      pa.caption:=name;  im:=Timage.create(form1);
      im.parent:=pa;
      im.name:='image'+inttostr(i);
      im.autosize:=true;
      if xb='男' then
        im.picture.bitbmp.loadformfile('c:\image\man.bmp')
      else
        im.picture.bitbmp.loadformfile('c:\image\man.bmp')//路径自己处理 ,这里写成了绝对路径
      im.left:=(pa.width-im.width) div 2;
      im.top:=(pa.height-im.height) div 2;
    end;上面panle和image的位置,自己根据实际需要修改就可以了
      

  4.   

    如果我已经在窗体中做了这么一个Panel. 请问:
    1. 怎么复制这个Panel?
    2. 怎么删除这个Panel?
    谢谢!
      

  5.   

    1、把panelold的各个主要属性赋值给新创建的panel
    2、panelold.free
      

  6.   

    1.复制这个panel
    1)首先创建一个panel,新创建这个panel,除了name其它属性全部用原panel属性赋值
    2)遍历原panel上所有控件,并在新panel上逐个创建,同样,除了name外其它全部用原控件属性赋值大体代码如下:(设原panel的name为panel1)  pa:=Tpanel.create(form1);
      pa.parent:=form1;
      pa.align:=panel1.align;
      pa.name:='panell'+inttostr(i);//这个i应该是全局变量,根据复制次数自动更新其值
      pa.caption:=panel1.caption;
      pa.left:=panel1.left;
      pa.top:=panel1.top;  m:=self.panel1.ControlCount;
      if m>0 then
      begin
        for n:=0 to m-1 do
        begin
          if self.panel1.Controls[n].ClassName<>'TImage' then
          begin
            im:=Timage.create(form1);
            im.parent:=pa;
            im.name:='imagee'+inttostr(i);
            im.autosize:=(self.panel1.Controls[n] as TImage).autosize;
            im.left:=(self.panel1.Controls[n] as TImage).left;
            im.top:=(self.panel1.Controls[n] as TImage).top;
            im.width:=(self.panel1.Controls[n] as TImage).width;
            im.height:=(self.panel1.Controls[n] as TImage).height;
            im.picture.bitbmp:=(self.panel1.Controls[n] as TImage).picture.bitbmp;
          end;
        end;
      end;
    2.panel1.free;
      

  7.   

    1. 我写了一个生成TBitBtn的代码:
    procedure TfrmWSview.CreateSickbed(BQID: string);
    var
      CWSQL, CWID: string;
      lTop, lLeft: Integer;
      btnX: TRzBitBtn;
    begin
      CWSQL:='select ID from BaseCW where BQID=:BQID order by ID';
      QCX.Close;
      QCX.SQL.Clear;
      QCX.SQL.Add(CWSQL);
      QCX.ParamByName('BQID').Value:=BQID;
      QCX.Open;
      while not QCX.Eof do
        begin
          for lTop:=0 to (pnBed.Height div 65) do
            for lLeft:=0 to (pnBed.Width div 57) do
              begin
                if QCX.Eof then Exit;            CWID:=QCX.FieldByName('ID').AsString;
                btnX:=TRzBitBtn.Create(Self);
                btnX.Parent:=pnBed;
                btnX.Layout:=blGlyphTop;
                btnX.Width:=57;
                btnX.Height:=65;
                btnX.HotTrack:=True;
                btnX.Top:=lTop*65;
                btnX.Left:=lLeft*57;
                btnX.Name:='btn'+CWID;
                btnX.Show;            QCX.Next;
              end;
        end;
    end;
    2.删除代码:
    procedure TfrmWSview.DestroySickbed;
    var
      i: Integer;
    begin
      for i:=0 to pnBed.ControlCount-1 do
        if (pnBed.Controls[i] is TRzBitBtn) then
          begin
            TRzBitBtn(pnBed.Controls[i]).Free;
          end;
    end;当生成40个TRzBitBtn后,执行删除代码时,提示'List index out of bounds(20)',出现的情况是只能隔一个删除一个TRzBitBtn.困扰中...
      

  8.   

    继续...
    之所以要删除这个生成的TRzBitBtn,是因为当我再次给BQID赋值后,生成TRzBitBtn时会提示相同名称的TRzBitBtn已经存在.