Creating components at runtime - Delphi

前端 未结 9 1890
孤街浪徒
孤街浪徒 2020-12-02 15:04

How can I create a component at runtime and then work with it (changing properties, etc.)?

9条回答
  •  清歌不尽
    2020-12-02 15:19

    To simplify the runtime component creation process, you can use GExperts.

    1. Create a component (or more components) visually and set its properties.
    2. Select one or more components and execute GExperts, Components to Code.
    3. Paste the generated code into your application.
    4. Remove component(s) from the visual form designer.

    Example (TButton-creation code generated in this way):

    var
      btnTest: TButton;
    
    btnTest := TButton.Create(Self);
    with btnTest do
    begin
      Name := 'btnTest';
      Parent := Self;
      Left := 272;
      Top := 120;
      Width := 161;
      Height := 41;
      Caption := 'Component creation test';
      Default := True;
      ParentFont := False;
      TabOrder := 0;
    end;
    

提交回复
热议问题