动态创建控件! SOS(100分)

  • 主题发起人 主题发起人 zyzdy
  • 开始时间 开始时间
Z

zyzdy

Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序中要动态创一个容器如Panel1,然后在上面创建一个控件。。

有个叫温柔一刀的,在不在,出来答题?
 
var a:Tpanel;
b:TLabel;
begin
a:=tpanel.create(self);
a.parent:=self;
b:=tlabel.create;
b.parent:=a;
end;
 
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

//____________________________________________________________________
//Project File Above


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure showMyMsg(Sender:TObject);
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
//Assume you will add a panel to a form and put a button on the panel
var
aForm:TForm;
aPanel:TPanel;
aButton:TButton;

begin
aForm:=Tform.create(application);
aForm.Top:=200;
aForm.Left:=200;
aForm.Width:=320;
aForm.Height:=200;
aForm.Caption:='this is a form generate by CODE';
{all other properties for this form can be specified Here}
aPanel:=TPanel.create(aForm);
aPanel.parent:=aForm;
aPanel.align:=alTop;
aPanel.caption:='';
{all other properties for this panel can specified Here}

aButton:=TButton.create(aForm);
aButton.parent:=aPanel;
aButton.caption:='click me';
aButton.OnClick:=showMyMsg;
aForm.ShowModal;

end;
procedure TForm1.showMyMsg(Sender: TObject);
begin
ShowMessage(Sender.ClassName+' is clicked at:'+DateTimeToStr(now) );
end;

end.
//____________________________________________________________________
//Unit file Above
 
多人接受答案了。
 
后退
顶部