1. 想动态创建timer 组件,代码如下  
xcd: array of TTimer;
procedure TForm1.Button1Click(Sender: TObject);
var
  i,j: integer;
begin
  for i := 0 to 2 do
    RemoveComponent(xcd[i]);
  xcd := nil ;
  label1.caption := inttostr(self.ComponentCount);
  setlength(xcd,3);
  for i :=0 to 2 do
  begin
    xcd[i] := Ttimer.create(self);
  end;
  label2.Caption := inttostr(self.ComponentCount);
end但是在第一个for循环就报错,为什么?2. 想动态创建某组组件,个数是不定的,
发现如果每次只是将动态数组置为nil时,对于动态数组本身是释放的,但是创建出来的组件仍然存在的,这个可以从两个label.caption中看出来,请问如何彻底把创建出来的组件释放掉3. 还有对于Tdxmemdata这个组件谁用过,里面的field如何用程序实现添加的?  女朋友这几天为这几个问题弄的心烦烦,给各位253了

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j: integer;
    begin
      for i := 0 to 2 do
        if Assigned(xcd[i]) then
          FreeAndNil(xcd[i]);
      label1.caption := inttostr(self.ComponentCount);
      setlength(xcd,3);
      for i :=0 to 2 do
      begin
        xcd[i] := Ttimer.create(self);
      end;
      label2.Caption := inttostr(self.ComponentCount);
    end
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Label2: TLabel;
        Timer1: TTimer;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      xcd: array of TTimer;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j: integer;
    begin
      for i := 0 to 2 do
        if Assigned(xcd[i]) then
          FreeAndNil(xcd[i]);
      label1.caption := inttostr(self.ComponentCount);
      setlength(xcd,3);
      for i :=0 to 2 do
      begin
        xcd[i] := Ttimer.create(self);
      end;
      label2.Caption := inttostr(self.ComponentCount);
    end ;procedure TForm1.FormCreate(Sender: TObject);
    begin
      setlength(xcd,3);
    end;end.
      

  3.   

    //linzhisong已经说了,xcd没有初始化~~
    //补充一下,用Low()和High()更方便一些~~
    var
      i, j: Integer;
    begin
      //第一次循环,xcd还没有初始化,所以访问xcd[0]、xcd[1]、xcd[2]都是错误的~~
      for i := Low(xcd) to High(xcd) do RemoveComponent(xcd[i]); //建议用 xcd[i].Free;
      //这就是第二个问题的答案,用循环释放数组里的元素
      Label1.Caption := IntToStr(ComponentCount);
      SetLength(xcd, 3);
      for i := Low(xcd) to High(xcd) do
      begin
        xcd[i] := TTimer.Create(Self);
      end;
      Label2.Caption := IntToStr(ComponentCount);
    end;
      

  4.   

    zswangII(伴水清清)(一贴不灌,何以灌天下?) (
    思想不错 收藏了