公司要求使用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.
但是无法显示,望指点一二,不胜感激!

解决方案 »

  1.   

    给你个例子,你少了一句:Parent := Self;
    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;
      

  2.   

    在你的程序里面加上btnTest.Parent := Self;就可以了
      

  3.   

    procedure TForm1.NewInstant;
    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;
      

  4.   

    总算成功了.
      btnTest:= TButton.Create(self);
      btnTest.Parent:= self;
    不过要再加一个Button,
    在FormCreate中调用似乎不行!