H
hwave
Unregistered / Unconfirmed
GUEST, unregistred user!
各位老师,请看代码:
unit CSBtn; //自定义能够动态生产控件的按钮
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TCSBtn = class(TButton)
private
{ Private declarations }
protected
procedure Click; override;
public
constructor Create(AOwner: TComponent); override; //构建
destructor Destroy; override;
published
{ Published declarations }
end;
procedure Register;
var
MyList: TList;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TCSBtn]);
end;
{ TCSBtn }
constructor TCSBtn.Create(AOwner: TComponent);
begin
inherited;
MyList := TList.Create;
end;
destructor TCSBtn.Destroy;
begin
MYList.Free;
inherited;
end;
procedure TCSBtn.Click;
var
mybutton: tbutton;
temp: integer;
begin
if mylist <> nil then
begin
mybutton := tbutton.create(self);
mybutton.parent := Self.parent;
temp := mylist.add(mybutton);
with mybutton do
begin
top := temp * 25;
left := 0;
caption := inttostr(temp);
end;
end;
end;
end.
//=================================================================
//在新开项目的form上放置数个Button,一个CSBtn
unit csU; //测试单元
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
CSBtn, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
CSBtn1: TCSBtn;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
for I := 0 to ComponentCount - 1 do
begin
if Components is Tbutton then
begin
Tbutton(Components).caption:='0';
end
end;
end;
end.
//=================================================================
单击CSBtn1按钮,动态生成数个Button,再点击Button1,设计时创建的Button的
caption的内容被置为 '0',而动态生成的Button没有变化,动态生成的button不在
Components的数组里,何故?怎样他们才能出现在Components数组中
unit CSBtn; //自定义能够动态生产控件的按钮
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TCSBtn = class(TButton)
private
{ Private declarations }
protected
procedure Click; override;
public
constructor Create(AOwner: TComponent); override; //构建
destructor Destroy; override;
published
{ Published declarations }
end;
procedure Register;
var
MyList: TList;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TCSBtn]);
end;
{ TCSBtn }
constructor TCSBtn.Create(AOwner: TComponent);
begin
inherited;
MyList := TList.Create;
end;
destructor TCSBtn.Destroy;
begin
MYList.Free;
inherited;
end;
procedure TCSBtn.Click;
var
mybutton: tbutton;
temp: integer;
begin
if mylist <> nil then
begin
mybutton := tbutton.create(self);
mybutton.parent := Self.parent;
temp := mylist.add(mybutton);
with mybutton do
begin
top := temp * 25;
left := 0;
caption := inttostr(temp);
end;
end;
end;
end.
//=================================================================
//在新开项目的form上放置数个Button,一个CSBtn
unit csU; //测试单元
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
CSBtn, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
CSBtn1: TCSBtn;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
for I := 0 to ComponentCount - 1 do
begin
if Components is Tbutton then
begin
Tbutton(Components).caption:='0';
end
end;
end;
end.
//=================================================================
单击CSBtn1按钮,动态生成数个Button,再点击Button1,设计时创建的Button的
caption的内容被置为 '0',而动态生成的Button没有变化,动态生成的button不在
Components的数组里,何故?怎样他们才能出现在Components数组中