unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables, ComCtrls;type
  TForm1 = class(TForm)
    ListView1: TListView;
    Database1: TDatabase;
    Query1: TQuery;
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  c:array of tcheckbox;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
    i:integer;
    list1:tlistitems;
begin
    with query1 do begin
        close;
        sql.Text:='select * from country.db';
        open;
        for i:=0 to query1.RecordCount-1 do begin
            c[i]:=tcheckbox.Create(nil);
            c[i].Parent:=self;
        end;
        for i:=0 to query1.RecordCount-1 do begin
            c[i].Free;
            c[i]:=nil;
        end;
    end;
end;
end.问题现在是c[i]:=tcheckbox.Create(nil);在此处出错!各位如何解决?

解决方案 »

  1.   

    这是个动态数据组,在使用前应该初始化这个数据:
    SetLength(c,Query1.RecordCount);
    将上面的语句放到Open的后面;还有用释放用c[i].Free就行了
      

  2.   

    数组c是动态数组还没有分配大小,用SetLength(c,Query1.RecordCount);
      

  3.   

    想要释放哪个的时候就用
    c[i].Free;
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
        i:integer;
    begin
        with query1 do begin
            close;
            sql.Text:='select * from country.db';
            open;
            SetLength(c,RecordCount);  // 设置数组大小
            for i:=0 to query1.RecordCount-1 do begin
                c[i]:=tcheckbox.Create(nil);
                c[i].Parent:=self;
            end;
            不要马上释入不然你什么都看不到了.
    end;
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    var
      i:=integer;
    begin
      for i:=Low(c) to High(c) do
      begin  
                c[i].Free;
                c[i]:=nil;
      end;
    end;
    end.