Y
ynfly
Unregistered / Unconfirmed
GUEST, unregistred user!
我通过继承Tpanel创建了一个控件TLabelPaenl,可以通过此控件的一个方法在上面增加Label;
我另外创建了一个工程Project1用来测试此控件,在project1的一个form上增加一个TLabelPanel,
命名为Lp,再增加一个Button,命名为Button1,在Button1上的click事件上写上代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
LP.addLabel ;
end;
现在我有两个问题:
1、我要求此Lp在打开时caption为空,而且我也在TLabelPanel的create事件上写上了,为何在使用时,
且显示Lp的caption为跟LP的Name的值一样?
2、如果通过Button1创建了100个Label的话,会产生错误,错误分别为:"project project1.exe raised
exception class EInvalidPointer with message 'Invalid pointer operation'.process stopped,
use step or run to continue ", "exception EInvalidpointer in module project1.exe at 00002794 ,
Invalid pointer operation" ,而在小于100的时候就没有错误,什么原因?
TLabelPanel组件代码如下:
unit LabelPanel;
interface
uses
Windows, Messages, SysUtils,dialogs, Classes, Controls, ExtCtrls,StdCtrls;
type
TLabelPanel = class(TPanel)
private
FcolumnNum:integer;//表示现在存在的Label个数;最大不可以超过100
{ Private declarations }
protected
{ Protected declarations }
public
NLabel:array[0..99] of TLabel;
procedure addLabel; //在Paenl上增加一个Label;
constructor create(Aowner:Tcomponent);override;
destructor destroy;override;
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('ActiveX', [TLabelPanel]);
end;
constructor TLabelPanel.create(Aowner:Tcomponent);
begin
inherited create(Aowner);
self.caption:='';
self.Width:=600;
self.Height:=480;
FcolumnNum:=0;
self.invalidate;
end;
destructor TLabelPanel.destroy ;
var intI:integer;
begin
if FcolumnNum>0 then
begin
//showmessage(inttostr(FcolumnNum));
for intI:=0 to FcolumnNum-1 do
begin
NLabel[intI].Free ;
invalidate;
end;
end;
inherited destroy;
end;
procedure TLabelPanel.addLabel;
var intX,intY:integer;
begin
if FcolumnNum<100 then
begin
intX:=FcolumnNum div 10;
intY:=FcolumnNum mod 10;
FcolumnNum:=FcolumnNum+1;
NLabel[FcolumnNum]:=TLabel.Create(self);
with NLabel[FcolumnNum] do
begin
parent:=self;
top:=intX*20+5;
left:=intY*30+5;
height:=20;
width:=30;
caption:=inttostr(FcolumnNum);
invalidate;
end;
end;
end;
end.
我另外创建了一个工程Project1用来测试此控件,在project1的一个form上增加一个TLabelPanel,
命名为Lp,再增加一个Button,命名为Button1,在Button1上的click事件上写上代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
LP.addLabel ;
end;
现在我有两个问题:
1、我要求此Lp在打开时caption为空,而且我也在TLabelPanel的create事件上写上了,为何在使用时,
且显示Lp的caption为跟LP的Name的值一样?
2、如果通过Button1创建了100个Label的话,会产生错误,错误分别为:"project project1.exe raised
exception class EInvalidPointer with message 'Invalid pointer operation'.process stopped,
use step or run to continue ", "exception EInvalidpointer in module project1.exe at 00002794 ,
Invalid pointer operation" ,而在小于100的时候就没有错误,什么原因?
TLabelPanel组件代码如下:
unit LabelPanel;
interface
uses
Windows, Messages, SysUtils,dialogs, Classes, Controls, ExtCtrls,StdCtrls;
type
TLabelPanel = class(TPanel)
private
FcolumnNum:integer;//表示现在存在的Label个数;最大不可以超过100
{ Private declarations }
protected
{ Protected declarations }
public
NLabel:array[0..99] of TLabel;
procedure addLabel; //在Paenl上增加一个Label;
constructor create(Aowner:Tcomponent);override;
destructor destroy;override;
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('ActiveX', [TLabelPanel]);
end;
constructor TLabelPanel.create(Aowner:Tcomponent);
begin
inherited create(Aowner);
self.caption:='';
self.Width:=600;
self.Height:=480;
FcolumnNum:=0;
self.invalidate;
end;
destructor TLabelPanel.destroy ;
var intI:integer;
begin
if FcolumnNum>0 then
begin
//showmessage(inttostr(FcolumnNum));
for intI:=0 to FcolumnNum-1 do
begin
NLabel[intI].Free ;
invalidate;
end;
end;
inherited destroy;
end;
procedure TLabelPanel.addLabel;
var intX,intY:integer;
begin
if FcolumnNum<100 then
begin
intX:=FcolumnNum div 10;
intY:=FcolumnNum mod 10;
FcolumnNum:=FcolumnNum+1;
NLabel[FcolumnNum]:=TLabel.Create(self);
with NLabel[FcolumnNum] do
begin
parent:=self;
top:=intX*20+5;
left:=intY*30+5;
height:=20;
width:=30;
caption:=inttostr(FcolumnNum);
invalidate;
end;
end;
end;
end.