TButton: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | A **TButton**  is a component that provides a basic push button control. It is available on the [[Standard tab]] of the [[Component Palette]]. | |
| 2 | 2 | ||
| 3 | - | ||
| 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. | |
| 4 | 4 | ||
| 5 | 5 | You can add a button to your form, by clicking the TButton (square button with an "OK" in the middle ) on the Standard component palette and place it with a click on your form. | |
| 6 | 6 | ||
| 7 | 7 | 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: | |
| 8 | 8 | ||
| 9 | 9 | ``` pascal | |
| 10 | 10 | procedure TForm1.Button1Click(Sender: TObject); | |
| 11 | 11 | begin | |
| 12 | 12 | ||
| 13 | 13 | end; | |
| 14 | 14 | ``` | |
| 15 | 15 | ||
| 16 | 16 | Between the [[statement|statements]] [[Begin|begin]] and [[End|end]] you could write code that is called when **Button1** is clicked. | |
| 17 | 17 | ||
| 18 | 18 | Almost 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: | |
| 19 | 19 | ||
| 20 | 20 | - [[Form Tutorial|The first GUI application]] for absolute beginners | |
| 21 | 21 | - [[Lazarus Tutorial|Your first Lazarus program]] tutorial for Lazarus | |
| 22 | 22 | - [[Object Oriented Programming with FreePascal and Lazarus|Programming Example]] Object Oriented Programming with Free Pascal and Lazarus | |
| 23 | 23 | ||
| 24 | 24 | ## A simple example | |
| 25 | 25 | ||
| 26 | 26 | - Create a new application and drop a TButton on the form. | |
| 27 | 27 | - Doubleclick this *Button1* on the form (the default handler: *OnClick* is created for *Button1*, the source text editor opens). | |
| 28 | 28 | - Add following code in the event handler: | |
| 29 | 29 | ||
| 30 | 30 | ``` pascal | |
| 31 | 31 | procedure TForm1.Button1Click(Sender: TObject); | |
| 32 | 32 | begin | |
| 33 | 33 | ShowMessage('Lazarus makes my day'); //A message will be displayed with the content... | |
| 34 | 34 | end; | |
| 35 | 35 | ``` | |
| 36 | 36 | ||
| 37 | - | ||
| 37 | + | - Start your program (with Key `F9`). | |
| 38 | 38 | ||
| 39 | 39 | ## Right-click | |
| 40 | 40 | ||
| 41 | 41 | Each TButton has an (optional) PopupMenu [[Property|property]] that will activate a connected [[TPopupMenu]] whenever the button is right-clicked. | |
| 42 | 42 | ||
| 43 | 43 | ## Dynamically generated button | |
| 44 | 44 | ||
| 45 | 45 | Sometimes, 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. | |
| 46 | 46 | ||
| 47 | 47 | This can be achieved as in the the following example (a quick calculator): | |
| 48 | 48 | ||
| 49 | 49 | - Create a new **blank** [[Form Tutorial|GUI application]] with the form *Form1* and add **StdCtrls** to the [[Uses|uses clause]] (here the TButton is). | |
| 50 | 50 | - Change caption *Form1* to *QuickAdd*. | |
| 51 | 51 | - Create the OnCreate event handler of Form1 (go in the Object Inspector to the event *OnCreate* and click the button \[...\]). | |
| 52 | 52 | - Add following code: | |
| 53 | 53 | ||
| 54 | 54 | ``` pascal | |
| 55 | 55 | procedure TForm1.FormCreate(Sender: TObject); | |
| 56 | 56 | var | |
| 57 | 57 | i: Integer; | |
| 58 | 58 | aButton: TButton; | |
| 59 | 59 | begin | |
| 60 | 60 | for i := 0 to 9 do begin // create 10 Buttons | |
| 61 | 61 | aButton := TButton.Create(Self); // create Button, Owner is Form1, where the button is released later | |
| 62 | 62 | aButton.Parent := Self; // determine where it is to be displayed | |
| 63 | 63 | aButton.Width := aButton.Height; // Width should correspond to the height of the buttons | |
| 64 | 64 | aButton.Left := i * aButton.Width; // Distance from left | |
| 65 | 65 | aButton.Caption := IntToStr(i); // Captions of the buttons (0.9) | |
| 66 | 66 | aButton.OnClick := @aButtonClick; // the event handler for the button -> will be created yet | |
| 67 | 67 | end; | |
| 68 | 68 | Self.Height := aButton.Height; // Height of the form should correspond to the height of the buttons | |
| 69 | 69 | Self.Width := aButton.Width * 10; // Width of the form to match the width of all buttons | |
| 70 | 70 | end; | |
| 71 | 71 | ``` | |
| 72 | 72 | ||
| 73 | 73 | - Now you must create the event handler for the button clicks. | |
| 74 | 74 | - In the source editor, entering your [[Class|class]] *TForm1* in the section [[Private|private]]. | |
| 75 | - | ||
| 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);`. | |
| 76 | 76 | - Paste following code: | |
| 77 | 77 | ||
| 78 | 78 | ``` pascal | |
| 79 | 79 | procedure TForm1.aButtonClick(Sender: TObject); | |
| 80 | 80 | const | |
| 81 | 81 | Cnt: Integer = 0; | |
| 82 | 82 | var | |
| 83 | 83 | i: Integer; | |
| 84 | 84 | begin | |
| 85 | 85 | if (Sender is TButton) and // called the event handler of a button out? | |
| 86 | 86 | TryStrToInt(TButton(Sender).Caption, i) // then try to convert the label in a integer | |
| 87 | 87 | then begin | |
| 88 | 88 | Cnt := Cnt + i; // the adding counter is incremented by the number of entrechende | |
| 89 | 89 | Caption:='QuickAdd: '+IntToStr(Cnt); // write the result to the caption of the form | |
| 90 | 90 | end; | |
| 91 | 91 | end; | |
| 92 | 92 | ``` | |
| 93 | 93 | ||
| 94 | 94 | - Start your application. | |
| 95 | 95 | ||
| 96 | 96 | > **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! | |
| 97 | 97 | ||
| 98 | 98 | ## See also | |
| 99 | 99 | ||
| 100 | 100 | - [[doc:lcl/stdctrls/tbutton.html|TButton doc]] | |
| 101 | 101 | - [[TBitBtn]] | |
| 102 | 102 | - [[TSpeedButton]] | |
| 103 | 103 | - [[TColorButton]] | |
| 104 | 104 | - [[15-puzzle]] (Game) | |
| 105 | 105 | ||
| 106 | 106 | --- | |
| 107 | 107 | *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).* |
