Sign in Join
Wiki copytemporary

TButton: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11A **TButton** ![](/wiki-images/b/b2/Tbutton.png) is a component that provides a basic push button control. It is available on the [[Standard tab]] of the [[Component Palette]].
22
3-A TButton is one of the most basic controls on a [[TForm|Form]]. Clicking with the mouse on it (or change with the <span class="kbd">Tab</span> key on the button and pressed it with <span class="kbd">Enter</span>), an action is triggered. This click is called an event. For this you need [[Event order|event handler]] that are called after the jump.
3+A TButton is one of the most basic controls on a [[TForm|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 order|event handler]] that are called after the jump.
44
55You can add a button to your form, by clicking the TButton (square button with an "OK" in the middle ![](/wiki-images/b/b2/Tbutton.png)) on the Standard component palette and place it with a click on your form.
66
77The 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:
88
99``` pascal
1010procedure TForm1.Button1Click(Sender: TObject);
1111begin
1212
1313end;
1414```
1515
1616Between the [[statement|statements]] [[Begin|begin]] and [[End|end]] you could write code that is called when **Button1** is clicked.
1717
1818Almost all available beginner tutorials use TButtons as an easy entry into the [[object-oriented programming|Object Oriented Programming]] with Lazarus. Following tutorials are well suited for beginners to understand the use of buttons:
1919
2020- [[Form Tutorial|The first GUI application]] for absolute beginners
2121- [[Lazarus Tutorial|Your first Lazarus program]] tutorial for Lazarus
2222- [[Object Oriented Programming with FreePascal and Lazarus|Programming Example]] Object Oriented Programming with Free Pascal and Lazarus
2323
2424## A simple example
2525
2626- Create a new application and drop a TButton on the form.
2727- Doubleclick this *Button1* on the form (the default handler: *OnClick* is created for *Button1*, the source text editor opens).
2828- Add following code in the event handler:
2929
3030``` pascal
3131procedure TForm1.Button1Click(Sender: TObject);
3232begin
3333 ShowMessage('Lazarus makes my day'); //A message will be displayed with the content...
3434end;
3535```
3636
37-- Start your program (with Key <span class="kbd">F9</span>).
37+- Start your program (with Key `F9`).
3838
3939## Right-click
4040
4141Each TButton has an (optional) PopupMenu [[Property|property]] that will activate a connected [[TPopupMenu]] whenever the button is right-clicked.
4242
4343## Dynamically generated button
4444
4545Sometimes, instead of creating buttons (or other components) with the Lazarus form designer, it is easier to create them dynamically at [[runtime|run time]]. This approach is useful especially if you have continually repeated buttons on a form.
4646
4747This can be achieved as in the the following example (a quick calculator):
4848
4949- Create a new **blank** [[Form Tutorial|GUI application]] with the form *Form1* and add **StdCtrls** to the [[Uses|uses clause]] (here the TButton is).
5050- Change caption *Form1* to *QuickAdd*.
5151- Create the OnCreate event handler of Form1 (go in the Object Inspector to the event *OnCreate* and click the button \[...\]).
5252- Add following code:
5353
5454``` pascal
5555procedure TForm1.FormCreate(Sender: TObject);
5656var
5757 i: Integer;
5858 aButton: TButton;
5959begin
6060 for i := 0 to 9 do begin // create 10 Buttons
6161 aButton := TButton.Create(Self); // create Button, Owner is Form1, where the button is released later
6262 aButton.Parent := Self; // determine where it is to be displayed
6363 aButton.Width := aButton.Height; // Width should correspond to the height of the buttons
6464 aButton.Left := i * aButton.Width; // Distance from left
6565 aButton.Caption := IntToStr(i); // Captions of the buttons (0.9)
6666 aButton.OnClick := @aButtonClick; // the event handler for the button -> will be created yet
6767 end;
6868 Self.Height := aButton.Height; // Height of the form should correspond to the height of the buttons
6969 Self.Width := aButton.Width * 10; // Width of the form to match the width of all buttons
7070end;
7171```
7272
7373- Now you must create the event handler for the button clicks.
7474- In the source editor, entering your [[Class|class]] *TForm1* in the section [[Private|private]].
75-- Add **`procedure aButtonClick(Sender: TObject);`** and then press the keys <span class="kbd">Ctrl</span>+<span class="kbd">Shift</span>+<span class="kbd">c</span> (the code completion becomes active and creates the [[Procedure|procedure]] `TForm1.aButtonClick(Sender: TObject);`.
75+- Add **`procedure aButtonClick(Sender: TObject);`** and then press the keys `Ctrl`+`Shift`+`c` (the code completion becomes active and creates the [[Procedure|procedure]] `TForm1.aButtonClick(Sender: TObject);`.
7676- Paste following code:
7777
7878``` pascal
7979procedure TForm1.aButtonClick(Sender: TObject);
8080const
8181 Cnt: Integer = 0;
8282var
8383 i: Integer;
8484begin
8585 if (Sender is TButton) and // called the event handler of a button out?
8686 TryStrToInt(TButton(Sender).Caption, i) // then try to convert the label in a integer
8787 then begin
8888 Cnt := Cnt + i; // the adding counter is incremented by the number of entrechende
8989 Caption:='QuickAdd: '+IntToStr(Cnt); // write the result to the caption of the form
9090 end;
9191end;
9292```
9393
9494- Start your application.
9595
9696> **Note:** You can assign every imaginable event handlers to your buttons, as long as this the form **`procedure <class>.<name of procedure>(Sender: TObject);`** has. Thus, you can use one from another class!
9797
9898## See also
9999
100100- [[doc:lcl/stdctrls/tbutton.html|TButton doc]]
101101- [[TBitBtn]]
102102- [[TSpeedButton]]
103103- [[TColorButton]]
104104- [[15-puzzle]] (Game)
105105
106106---
107107*From the Free Pascal wiki, page "TButton", as it stood on 2019-11-09 after a change by Djzepi, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history