unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    ButtonList: TList;
    procedure ButtonClick(Sender: TObject);
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
    ButtonList:=TList.Create;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
    ButtonList.Free;
end;procedure TForm1.Button1Click(Sender: TObject);
var Button: TButton;
begin
    Button:=TButton.Create(Self);
    Button.Top:=ButtonList.Count*30+20;
    Button.Left:=70;
    Button.Parent:=Self;
    Button.Caption:='按钮【'+IntToStr(ButtonList.Count+1)+'】';
    Button.OnClick:=ButtonClick;
    ButtonList.Add(Button);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
    TButton((ButtonList.Items[StrToInt(Edit1.Text)])^).Height:=100;
// 在这里为什么会出错?
end;procedure TForm1.ButtonClick(Sender: TObject);
begin
    ShowMessage(TButton(Sender).Caption);
end;end.以上代码,为什么在我调用
TButton((ButtonList.Items[StrToInt(Edit1.Text)])^).Height:=100;
时会出错?

解决方案 »

  1.   

    TButton(ButtonList.Items[StrToInt(Edit1.Text)]).Height:=100;
      

  2.   

    你出错的原因是因为List中的元素没有Edit1中那么多,所以出现边界超出异常
    你把代码变成这样,可以保证不出错
    procedure TForm1.Button2Click(Sender: TObject);
    begin
       if ButtonList.Count> StrToInt(Edit1.Text) then
      TButton((ButtonList.Items[StrToInt(Edit1.Text)-1])).Height:=100;
    end;
    然后如果你的Edit1输入的数是3,如果你要按Button1三次以上,即生成三个按钮以上,即List中有三个以后的元素,然后按Button2就能看到效果。
      

  3.   

    不好意思,上面的if ButtonList.Count> StrToInt(Edit1.Text) then
    改成if ButtonList.Count>=StrToInt(Edit1.Text) then这样,才可以操作所有的按钮。
    不然最后一个按钮操作不到。
      

  4.   

    to: linzhengqun(linzhengqun) 我当然会避免 ButtonList.Count >= StrToInt(Edit1.Text) 这种情况!!!即使我在 Edit1 中输入 0 ,添加了 10 个按钮也一样会出错!
      

  5.   

    奇怪了!我记得我测试过不加^的情况,一样会出错!
    怎么我刚才用这句测试又正常了:
    TButton(ButtonList.Items[StrToInt(Edit1.Text)]).Height:=100;好吧!就算我搞错了!
    那么谁能告诉我为什么用下面这句不正确?
    TButton((ButtonList.Items[StrToInt(Edit1.Text)])^).Height:=100;
      

  6.   

    还有为什么这样用也不正确?
    type PButton = ^TButton;
    begin
        PButton(ButtonList.Items[StrToInt(Edit1.Text)]).Width:=100;
    end;
      

  7.   

    不需要指针的啊,只是一个强制转换,你这一句
    type PButton = ^TButton;
    begin
        PButton(ButtonList.Items[StrToInt(Edit1.Text)]).Width:=100;
    end;
    和这一句TButton((ButtonList.Items[StrToInt(Edit1.Text)])^).Height:=100;
    是等价的,
    TList可以存指针,也可以存对象。所以你根本不用指针,直接把对象存进去,然后用的时候,直接取出来再进行一次强制转换就行了,或者你也可以用这样的形式,也许更直观一些:
        var button1:TButton;
        begin
            Button1:=ButtonList.Items[StrToInt(Edit1.Text)-1];
            Button.Width:=100;
        end;接着你说:“我记得我测试过不加^的情况,一样会出错!”
    原因只有一个,边界超出,为什么,因为你用这一句啊,
    TButton(ButtonList.Items[StrToInt(Edit1.Text)]).Height:=100;
    你当时肯定是在Edit中输入比如5,然后你按Button1五次,即TList有五个元素
    好,你再按Button2,这时调用这句
    TButton(ButtonList.Items[StrToInt(Edit1.Text)]).Height:=100;,就出错了,对吗。
    Tlist是多0开始计数的,如果Edit里的内容是5,但ButtonList.Items[StrToInt(Edit1.Text)]则是第六个元素了,当然会出错。
    如果你按Button1的次数多于Edit1.text,就不会出错了。
    所以你必须这样写才是正确的:
    TButton(ButtonList.Items[StrToInt(Edit1.Text)-1]).Height:=100;
    明白了吗,里面有减1的
      

  8.   

    我终于知道为什么下面这句不加^的情况会出错!
    (不是因为边界超出,我也注意了减一不减一的差别)
    TButton(ButtonList.Items[StrToInt(Edit1.Text)]).Height:=100;因为我以前用了这句:ButtonList.Add(@Button);既然对象名就是指针那么 ButtonList.Add(@Button); 和 ButtonList.Add(Button); 
    又有什么差别呢?好了!既然我用了这句:ButtonList.Add(@Button);
    那么下面我用这句行不行: 
    type PButton = ^TButton;
    begin
        PButton(ButtonList.Items[StrToInt(Edit1.Text)]).Width:=100;
    end;
    实践证明同样是错误的,但是为什么呢?
      

  9.   

    其实不用那么复杂,你只要ButtonList.Add(Button);就行了,
    因为TList可以存对象,也可以存指针,而,对象也不是什么指针。
    只是Pointer的特性,
    你只要记住,他可以存对象,也可以存指针
    而存对象的时候,只要List.add(YouObject)。就可以了
    操作的时候也是直接操作对象就可以了,不用涉及到指针的。另外Button是一个对象 @Button是这个对象的在内存中的地址
      

  10.   

    说实话,你应该进入TList内部看看,你的程序就会简单多了!TList的增 删 加 移 插等操作都是现成的!
      

  11.   

    wgqcsdn (wgq) 兄弟,如果没有什么问题了,就把贴给结了吧。拖着也不是办法呀,当造福一下我们吧。