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