Delphi 動態創建控件,並綁定控件事件


type
  TForm13 = class(TForm)
    Button1: TButton;
    ScrollBox1: TScrollBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure myClick(sender: TObject);
  public
    { Public declarations }
  end;

var
  Form13: TForm13;

implementation

{$R *.dfm}

procedure TForm13.Button1Click(Sender: TObject);
const
  iHtoH = 20; //行間距
  iWtoW = 10; //列間距
  iPerLine = 6; //每行個數
  iWidth = 80; //按鈕寬度
var
  i: Integer;
begin
  for i := 1 to 30 do
  begin
    with TButton.Create(Self) do
    begin
      Name := 'Btn' + InttoStr(i);
      Parent := Self.ScrollBox1;
      Caption := Name;
      Top := iHtoH + (30 + iHtoH) * (i div iPerLine - integer((i mod iPerLine) = 0));
      Left := iWtoW + (iWidth + iWtoW) * ((i - 1) mod iPerLine);
      if Name = 'Btn5' then
      begin
        OnClick := myClick;
      end;
      Show;
    end;
  end;
end;

procedure TForm13.myClick(sender: TObject);
begin
  ShowMessage('hello,world!');
end;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM