相关代码如下:procedure TForm2.ComboBox1Change(Sender: TObject);
var
  i,j,count:integer;
  tt:array of Tedit;
begin
  j:= Panel1.ControlCount;
  while Panel1.ControlCount >0 do begin
  Panel1.Controls[j-1].Free;
  j:=j-1;
  end;
  count:=strtoint(combobox1.items[combobox1.ItemIndex]);
  for i:=0 to count-1 do
  begin
    tt[i]:=Tedit.Create(self);
    tt[i].Visible:=true;
    tt[i].left:=10;
    tt[i].parent:=Panel1;
    tt[i].top:=30*i+10;
  end;
end;编译通过了,但是运行时提醒错误:“tt”might not have been intialized.

解决方案 »

  1.   

    【补充】显示错误的地方是:tt[i]:=Tedit.Create(self);
      

  2.   

    ding!ding!ding!ding!ding!ding!ding!ding!ding!ding!
      

  3.   

    ...
    ...
    setlength(tt,count-1);
    for i:= ...
      

  4.   


    procedure TForm2.ComboBox1Change(Sender: TObject);
    var
      i,j,count:integer;
      tt:array of Tedit;
    begin
      j:= Panel1.ControlCount;
      while Panel1.ControlCount >0 do 
      begin
      Panel1.Controls[j-1].Free;
      j:=j-1;
      end;
      count:=strtoint(combobox1.items[combobox1.ItemIndex]);
      setlength(tt,count-1); //动态数组使用前要设置大小
      for i:=0 to count-1 do
      begin
        tt[i]:=Tedit.Create(self);
        tt[i].Visible:=true;
        tt[i].left:=10;
        tt[i].parent:=Panel1;
        tt[i].top:=30*i+10;
      end;
    end;
      

  5.   

    setlength(tt,count-1); //动态数组使用前要设置大小
      

  6.   

    动态数组由于定义时并没有声明数组大小,因此Delphi并没有为动态数组正式分配内存,所以在正式使用之前必须设置数组大小,否则使用时将引起错误
    setlength(tt, count); //动态数组使用前要设置大小
      

  7.   

    setlength(tt, count);
    语句中count是指定的新大小值,而在索引时由于动态数组是0起始的,所以最大索引值是count- 1
      

  8.   

    我正准备说呢,哪知道这么多高手都说过了。
    就是没有SetLength