Difference between revisions of "TButton"
(new "Work with a button") |
(add "Dynamically generated button") |
||
Line 30: | Line 30: | ||
* [[Lazarus_Tutorial#Your_first_Lazarus_Program.21|Your first Lazarus program]] tutorial for Lazarus | * [[Lazarus_Tutorial#Your_first_Lazarus_Program.21|Your first Lazarus program]] tutorial for Lazarus | ||
* [[Object_Oriented_Programming_with_FreePascal_and_Lazarus#Programming_Example|Programming Example]] Object Oriented Programming with Free Pascal and Lazarus | * [[Object_Oriented_Programming_with_FreePascal_and_Lazarus#Programming_Example|Programming Example]] Object Oriented Programming with Free Pascal and Lazarus | ||
+ | |||
+ | ==Dynamically generated button== | ||
+ | |||
+ | Sometimes it is easier, the buttons (or other components) not with the Lazarus form designer to create, but dynamically at run time. This approach is useful especially if you have continually repeated buttons on a form. | ||
+ | |||
+ | Works like this you can adjust the following example (a quick calculator): | ||
+ | * Create a new '''blank''' [[Form_Tutorial#The_first_GUI_application|GUI application]] with the form ''Form1''. | ||
+ | * Change caption ''Form1'' to ''QuickAdd''. | ||
+ | * Create the OnCreate event handler of Form1 (go in the Object Inspector to the event ''OnCreate'' and click the button [...]). | ||
+ | * Add following code: | ||
+ | <syntaxhighlight> | ||
+ | procedure TForm1.FormCreate(Sender: TObject); | ||
+ | var | ||
+ | i: Integer; | ||
+ | aButton: TButton; | ||
+ | begin | ||
+ | for i:=0 to 9 do begin //create 10 Buttons | ||
+ | aButton:=TButton.Create(Self); //create Button, Owner is Form1, where the button is released later | ||
+ | aButton.Parent:=Self; //determine where it is to be displayed | ||
+ | aButton.Width:=aButton.Height; //Width should correspond to the height of the buttons | ||
+ | aButton.Left:=i*aButton.Width; //Distance from left | ||
+ | aButton.Caption:=IntToStr(i); //Captions of the buttons (0.9) | ||
+ | aButton.OnClick:=@aButtonClick; //the event handler for the button -> will be created yet | ||
+ | end; | ||
+ | Self.Height:=aButton.Height; //Height of the form should correspond to the height of the buttons | ||
+ | Self.Width:=aButton.Width*10; //Width of the form to match the width of all buttons | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | * Now you must create the event handler for the button clicks. | ||
+ | * In the source editor, entering your class ''TForm1'' in the section '''<code>private</code>'''. | ||
+ | * Add '''<code>procedure aButtonClick(Sender: TObject);</code>''' and then press the keys [CTRL] + [Shift] + [c] (the code completion becomes active and creates the procedure <code>TForm1.aButtonClick(Sender: TObject);</code>. | ||
+ | * Paste following code: | ||
+ | <syntaxhighlight> | ||
+ | procedure TForm1.aButtonClick(Sender: TObject); | ||
+ | const | ||
+ | Cnt: Integer = 0; | ||
+ | var | ||
+ | i: Integer; | ||
+ | begin | ||
+ | if (Sender is TButton) //called the event handler of a button out? | ||
+ | and TryStrToInt(TButton(Sender).Caption, i) then //then try to convert the label in a integer | ||
+ | begin | ||
+ | Cnt:=Cnt + i; //the adding counter is incremented by the number of entrechende | ||
+ | Caption:='QuickAdd: '+IntToStr(Cnt); //write the result to the caption of the form | ||
+ | end; | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | * Start your application. | ||
Revision as of 13:52, 15 May 2014
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
>> LCL Components >> TButton
This page explains how to use the TButton component. When I mention to click on something, unless I explicitly say to right-click, you always left-click on the item in question.
Description
A push button control.
Work with a button
A button is one of the most basic controls on a Form. Clicking with the mouse on it (or change with the [tab] key on the button and pressed it with enter), an action is triggered. This click is called an event. For this you need event handler that are called after the jump.
The event handler for a mouse click can be quite easily reached in which a double-click on the pasted Button (or in the Object Inspector, select the event OnClick of your Button). The event handler for a Button1 on a form Form1 will look like this:
procedure TForm1.Button1Click(Sender: TObject);
begin
end;
Between the statements begin and end you could write code that is called when Button1 is clicked.
Almost all available beginner tutorials use TButtons as an easy entry into the Object Oriented Programming with Lazarus. Following tutorials are well suited for beginners to understand the use of buttons:
- The first GUI application for absolute beginners
- Your first Lazarus program tutorial for Lazarus
- Programming Example Object Oriented Programming with Free Pascal and Lazarus
Dynamically generated button
Sometimes it is easier, the buttons (or other components) not with the Lazarus form designer to create, but dynamically at run time. This approach is useful especially if you have continually repeated buttons on a form.
Works like this you can adjust the following example (a quick calculator):
- Create a new blank GUI application with the form Form1.
- Change caption Form1 to QuickAdd.
- Create the OnCreate event handler of Form1 (go in the Object Inspector to the event OnCreate and click the button [...]).
- Add following code:
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
aButton: TButton;
begin
for i:=0 to 9 do begin //create 10 Buttons
aButton:=TButton.Create(Self); //create Button, Owner is Form1, where the button is released later
aButton.Parent:=Self; //determine where it is to be displayed
aButton.Width:=aButton.Height; //Width should correspond to the height of the buttons
aButton.Left:=i*aButton.Width; //Distance from left
aButton.Caption:=IntToStr(i); //Captions of the buttons (0.9)
aButton.OnClick:=@aButtonClick; //the event handler for the button -> will be created yet
end;
Self.Height:=aButton.Height; //Height of the form should correspond to the height of the buttons
Self.Width:=aButton.Width*10; //Width of the form to match the width of all buttons
end;
- Now you must create the event handler for the button clicks.
- In the source editor, entering your class TForm1 in the section
private
. - Add
procedure aButtonClick(Sender: TObject);
and then press the keys [CTRL] + [Shift] + [c] (the code completion becomes active and creates the procedureTForm1.aButtonClick(Sender: TObject);
. - Paste following code:
procedure TForm1.aButtonClick(Sender: TObject);
const
Cnt: Integer = 0;
var
i: Integer;
begin
if (Sender is TButton) //called the event handler of a button out?
and TryStrToInt(TButton(Sender).Caption, i) then //then try to convert the label in a integer
begin
Cnt:=Cnt + i; //the adding counter is incremented by the number of entrechende
Caption:='QuickAdd: '+IntToStr(Cnt); //write the result to the caption of the form
end;
end;
- Start your application.
Return To: LCL Components | — Previous: TPopupMenu | Next: TLabel |
--Michl 12:25, 15 May 2014 (CEST)