delphi中timage和tlabel动态数组的问题

delphi中timage和tlabel动态数组的问题,delphi,Delphi,我想添加一个附件,并让表单在每次添加附件时都变长,以便为一行留出空间,该行包含关于带有标签和一些16X16图像的附件的信息。为此,我选择使用动态数组(不确定这是否是最好的)。每次添加附件时,我都要创建这些对象的新实例。我的代码似乎不起作用。下面的代码怎么了 procedure TVisionMail.AddAttachment(FileString: String); var I: Integer; begin AttCount := AttCount + 1; // incremen

我想添加一个附件,并让表单在每次添加附件时都变长,以便为一行留出空间,该行包含关于带有标签和一些16X16图像的附件的信息。为此,我选择使用动态数组(不确定这是否是最好的)。每次添加附件时,我都要创建这些对象的新实例。我的代码似乎不起作用。下面的代码怎么了

procedure TVisionMail.AddAttachment(FileString: String);
var
I: Integer;
begin
     AttCount := AttCount + 1; // increment attachment count

     //set attachment file name
     if (AttCount <> 0) and (edAttachment.Text <> '') then
       edAttachment.text := edAttachment.text + ';';
     edAttachment.text := edAttachment.text + FileString;

     //move objects position down to allow space for attachment line
     VisionMail.Height := VisionMail.Height + 25;
     Panel1.Height     := Panel1.Height + 25;
     btnSend.Top       := btnSend.Top + 25;
     btnExit.Top       := btnExit.Top + 25;
     StatusMemo.Top    := StatusMemo.Top + 25;
     Memo1.Top         := Memo1.Top + 25;
     lblBody.Top       := lblBody.Top + 25;

       //Allocate memory for arrays
       SetLength(newImg, AttCount);
       SetLength(newlbl, AttCount);
       SetLength(newDel, AttCount);
       SetLength(newPin, AttCount);

        //create new instance and set parents, positions, color, events
        newImg[AttCount]:= TImage.Create(VisionMail);
        with newImg[AttCount] do
        begin
              Parent     := Panel1;
              Top        := Memo1.Top - 25;
              Left       := 408;
              Height     := 16;
              Width      := 16;
        end;
        newlbl[AttCount]:= TLabel.Create(VisionMail);
        with newlbl[AttCount] do
        begin
              Parent     := Panel1;
              Top        := newImg[I].Top + 2;
              Left       := 397;
              Height     := 3;
              Width      := 13;
              BiDiMode   := bdRightToLeft;
       end;
       newDel[AttCount] := TAdvToolButton.Create(VisionMail);
       with newDel[AttCount] do
        begin
              Parent       := Panel1;
              Top          := newImg[I].Top;
              Left         := 440;
              Height       := 16;
              Width        := 16;
              color        := clBtnFace;
              colorChecked := clBtnFace;
              colorDown    := clBtnFace;
              colorHot     := clBtnFace;
              OnClick      := btnDelAttClick;
              OnMouseEnter := btnDelAttMouseEnter;
              OnMouseLeave := btnDelAttMouseLeave;
       end;
       newPin[AttCount] := TImage.Create(VisionMail);
       with newDel[AttCount] do
        begin
              Parent     := Panel1;
              Top        := newImg[I].Top;
              Left       := 425;
              Height     := 16;
              Width      := 16;
       end;
       //get Icon for extension of file
       lstIcons.GetBitmap(GetIcon(ExtractFileExt
                          (OpenDialog1.FileName)),
                          newImg[AttCount].Picture.Bitmap);
       newlbl[AttCount].Caption    := ExtractFileName(FileString);

end; 
过程TVisionMail.AddAttachment(FileString:String);
变量
I:整数;
开始
ATTCUNT:=ATTCUNT+1;//增量附件计数
//设置附件文件名
如果(AttCount 0)和(edAttachment.Text“”),则
eAttachment.text:=eAttachment.text+';';
eAttachment.text:=eAttachment.text+文件字符串;
//向下移动对象位置,以便为附着线留出空间
VisionMail.Height:=VisionMail.Height+25;
面板1.高度:=面板1.高度+25;
btnSend.Top:=btnSend.Top+25;
btnExit.Top:=btnExit.Top+25;
StatusMemo.Top:=StatusMemo.Top+25;
Memo1.Top:=Memo1.Top+25;
lblBody.Top:=lblBody.Top+25;
//为阵列分配内存
设置长度(newImg,AttCount);
设置长度(newlbl,AttCount);
设置长度(newDel,AttCount);
设置长度(newPin、AttCount);
//创建新实例并设置父对象、位置、颜色和事件
newImg[AttCount]:=TImage.Create(VisionMail);
使用newImg[AttCount]do
开始
父项:=1;
Top:=1.Top-25;
左:=408;
高度:=16;
宽度:=16;
结束;
newlbl[AttCount]:=TLabel.Create(VisionMail);
使用newlbl[AttCount]do
开始
父项:=1;
Top:=newImg[I]。Top+2;
左:=397;
高度:=3;
宽度:=13;
BiDiMode:=bdright-toleft;
结束;
newDel[AttCount]:=TAdvToolButton.Create(VisionMail);
用newDel[AttCount]做
开始
父项:=1;
Top:=newImg[I]。Top;
左:=440;
高度:=16;
宽度:=16;
颜色:=clBtnFace;
颜色检查:=clBtnFace;
颜色降低:=clBtnFace;
colorHot:=clBtnFace;
OnClick:=btnDelAttClick;
OnMouseEnter:=btndelatetmouseenter;
OnMouseLeave:=btndelatemMouseLeave;
结束;
newPin[AttCount]:=TImage.Create(VisionMail);
用newDel[AttCount]做
开始
父项:=1;
Top:=newImg[I]。Top;
左:=425;
高度:=16;
宽度:=16;
结束;
//获取文件扩展名的图标
GetBitmap(GetIcon(ExtractFileExt
(OpenDialog1.FileName)),
newImg[AttCount].Picture.Bitmap);
newlbl[AttCount]。标题:=ExtractFileName(FileString);
结束;

最明显的缺陷是您正在注销所有数组的末尾。例如,你写

SetLength(newImg, AttCount);
这意味着
newImg
的有效指数为
0
AttCount-1
包括在内。然后你就开始写了

newImg[AttCount] := ...
这是一个越界访问,因为最后一个索引是
AttCount-1
。对所有阵列访问都执行相同的操作

如果您在启用范围检查的情况下编译,编译器将生成一个运行时错误,解释您所做的错误

就我个人而言,我认为你最好用一个记录来保存你的四个组成部分:

TAttachmentControls = record
  Img: TImage;
  Lbl: TLabel;
  .. etc.
end;

并使用
t列表
作为容器。

您的数据结构完全错误。您需要一个记录来保存这4个组件。然后
TList
保存所有记录。现在,您还声明“我的代码似乎不起作用”。那对我们没有用。你必须描述它是如何失败的。准确地解释您对代码的期望,以及它以何种方式无法满足该期望。这将花费你更多的时间。节省我们的时间。一定是这样的。