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