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;
时会出错?
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;
时会出错?
解决方案 »
- 辽宁北软沈阳分公司于2007/1/16日开张,急聘delphi项目经理、工程师
- tchart中制图问题
- TDX开头的是什么?哪里有下?例如:TDXButton, TDXCheckBox, TDXComboBox, TDXEdit, TDXGauge, TDXLabel
- 如何得到sql表在插入时自动加一的id值?
- 还没毕业,找到实习单位,公司说刚开始一个月800,包住宿。大家觉得该不该干?另调查:你的工资多少?
- 请问在delphi重实现ip数据报的捕获和分析?
- delphi问题高手赐教(事件的区别)
- 怎样在程序中调用文本文件呢?
- 超难!!!怎么用 EnumDesktopWindows 对桌面上的所有窗体进行枚举???
- 请各位帮我看看这段代码
- 分页的问题,急急急。。请各位高手帮忙,解决了给一百分酬谢!!!!
- 如何引用第三方控件(马上结贴)
你把代码变成这样,可以保证不出错
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就能看到效果。
改成if ButtonList.Count>=StrToInt(Edit1.Text) then这样,才可以操作所有的按钮。
不然最后一个按钮操作不到。
怎么我刚才用这句测试又正常了:
TButton(ButtonList.Items[StrToInt(Edit1.Text)]).Height:=100;好吧!就算我搞错了!
那么谁能告诉我为什么用下面这句不正确?
TButton((ButtonList.Items[StrToInt(Edit1.Text)])^).Height:=100;
type PButton = ^TButton;
begin
PButton(ButtonList.Items[StrToInt(Edit1.Text)]).Width:=100;
end;
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的
(不是因为边界超出,我也注意了减一不减一的差别)
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;
实践证明同样是错误的,但是为什么呢?
因为TList可以存对象,也可以存指针,而,对象也不是什么指针。
只是Pointer的特性,
你只要记住,他可以存对象,也可以存指针
而存对象的时候,只要List.add(YouObject)。就可以了
操作的时候也是直接操作对象就可以了,不用涉及到指针的。另外Button是一个对象 @Button是这个对象的在内存中的地址