[Tbutton] Running Dynamic creation and delete button

xiaoxiao2021-03-06  59

Newly open a project, then drag two Button on the form code as follows: Unit unit1;

Interface

Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;

type TForm1 = class (TForm) btnAddButton: TButton; btnDeleteLast: TButton; procedure btnAddButtonClick (Sender: TObject); procedure btnDeleteLastClick (Sender: TObject); private {Private declarations} procedure CustomButtonClick (Sender: TObject); public {Public declarations} end ;

Var Form1: TFORM1;

IMPLEMENTATION

{$ R * .dfm}

Procedure TForm1.btnaddButtonClick (Sender: TOBJECT); VAR NewButton: TButton; // New Button's pointer begin // Create a button in memory, the owner is self, so when the form Destory, this new Button // can be Automatically release newbutton: = tbutton.create (Self);

WITH NEWBUTTON DO BEGIN TOP: = 60; // Button's appearance width: = 60; // Button's width Left: = width * (Self.ControlCount - 2); Parent: = Self; // Indicate in that window Body display onclick: = CustomButtonClick; // Specify button Click event CAPTION: = 'Button' INTOSTR (Self.ControlCount - 2); end; // wirth;

Procedure tForm1.btndeletelastClick (Sender: TOBJECT); Begin // Determines the new Button if Self.ControlCount> 2 THEN / / Delete the last new button TButton (Controls [ControlCount - 1]). design.

Procedure TForm1.custom (Sender: TOBJECT); Begin // Depending on Sender to determine which new Button Click ShowMessage (TButton (Sender) .caption 'PRESSED');

end of:. lzcx

转载请注明原文地址:https://www.9cbs.com/read-114172.html

New Post(0)