公司要求使用delphi,努力学习中。想自己做个扫雷游戏,其间想动态创建按钮数组,请问该如何实现?
我的代码如下:
unit UBtnTest;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
btnTest: TButton;
{ Private declarations }
public
procedure NewInstant;
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.NewInstant;
begin
btnTest:= TButton.Create(Form1);
btnTest.Left:= 0;
btnTest.Width:=50;
btnTest.Top:= 0;
btnTest.Height:= 25;
// btnTest.Show;
btnTest.Caption:= '开始';
btnTest.Visible:= true;
btnTest.Repaint;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
NewInstant;
end;
end.
但是无法显示,望指点一二,不胜感激!
我的代码如下:
unit UBtnTest;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
btnTest: TButton;
{ Private declarations }
public
procedure NewInstant;
{ Public declarations }
end;var
Form1: TForm1;implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.NewInstant;
begin
btnTest:= TButton.Create(Form1);
btnTest.Left:= 0;
btnTest.Width:=50;
btnTest.Top:= 0;
btnTest.Height:= 25;
// btnTest.Show;
btnTest.Caption:= '开始';
btnTest.Visible:= true;
btnTest.Repaint;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
NewInstant;
end;
end.
但是无法显示,望指点一二,不胜感激!
procedure TForm1.Button1Click(Sender: TObject);
begin
with TButton.Create(self) do
begin
Name := 'btnTest';
Caption := '测试';
Left := 0;
Top := 0;
Width := 100;
Height := 25;
Parent := Self;
end;
end;
begin
btnTest:= TButton.Create(Self);
btnTest.Parent := Self; //你的代码少了这一句
btnTest.Left:= 0;
btnTest.Width:=50;
btnTest.Top:= 0;
btnTest.Height:= 25;
btnTest.Caption:= '开始';
btnTest.Visible:= True;
end;
btnTest:= TButton.Create(self);
btnTest.Parent:= self;
不过要再加一个Button,
在FormCreate中调用似乎不行!