TLabel: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TLabel
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TLabel**  is a component that creates a text-item with one or more lines on another component. A TLabel is a descendant of [[TGraphicControl]] and is available under the [[Standard tab]] of the [[Component Palette]]. | |
| 2 | + | ||
| 3 | + | ## Usage | |
| 4 | + | ||
| 5 | + | A TLabel is one of the most basic components that can be used on [[TForm|forms]]. Most labels are the marking of other components, such as [[TEdit|Edit fields]], [[TMemo|Memos]], [[TStringGrid|StringGrids]] and so on. TLabels provide a variety of events available to you but not required in most cases. | |
| 6 | + | ||
| 7 | + | You can add a label to your form, by clicking the TLabel (text symbol Abc) on the Standard component palette and place it with a click on your form. | |
| 8 | + | ||
| 9 | + | To change the default label *Label* of a new inserted TLabel on a form, you can proceed as follows: | |
| 10 | + | ||
| 11 | + | - On your form with one click, select the TLabel. | |
| 12 | + | - Go on properties in the Object Inspector tab. | |
| 13 | + | - Select the property **Caption** and change it in the next edit field. | |
| 14 | + | - In the same way, you can select the property **Name** and give the label a better name. | |
| 15 | + | ||
| 16 | + | ## Change caption at run time | |
| 17 | + | ||
| 18 | + | Of course, you can change the caption (the text displayed) during run time. | |
| 19 | + | ||
| 20 | + | The following example demonstrates this: | |
| 21 | + | ||
| 22 | + | - Create a new [[Form Tutorial|GUI application]] with the form *Form1*. Add this form still a [[TButton]] *Button1* and a TLabel *Label1* by selecting the appropriate components on the Standard Component Tab and clicking on *Form1* (the label should be above the button). | |
| 23 | + | - Create now a [[Event order|event handler]] for *Button1*, by simply double clicking on *Button1*. | |
| 24 | + | - Insert following lines of code in the OnClick event handler of *Button1*: | |
| 25 | + | ||
| 26 | + | ``` pascal | |
| 27 | + | procedure TForm1.Button1Click(Sender: TObject); | |
| 28 | + | const Cnt: Integer = 0; //Counter to determine how many times the button has been clicked | |
| 29 | + | begin | |
| 30 | + | inc(Cnt); //Increment the counter by 1 | |
| 31 | + | Label1.Caption:='Button was clicked ' + //Write the text on the caption of Label1 | |
| 32 | + | IntToStr(Cnt) + ' times'; | |
| 33 | + | end; | |
| 34 | + | ``` | |
| 35 | + | ||
| 36 | + | - Start your program and test the change of the label caption by clicking the button. | |
| 37 | + | ||
| 38 | + | ## Delphi incompatibility for right-aligned labels | |
| 39 | + | ||
| 40 | + | - In Delphi, autosized labels with Alignment=taRightJustify but Anchors=\[akLeft,...\] grow to left. In LCL they grow to to right, starting with Lazarus 2.3.0. | |
| 41 | + | - Reason: It wasn't possible to implement the behavior also for hidden labels without significant extensions in the LCL. The LCL has a different and more generic feature of control-based anchoring that delivers the same effect (see Remedy down), so it is not needed and wanted to double this feature and make the LCL code more complex and prone to bugs. | |
| 42 | + | - Remedy: Use the LCL anchoring to a secondary control. Anchor the right side of the label to another control. Then the autosized label will grow to the left but won't move to the right when the parent is resized like it is done with a simple akRight anchor without a reference control. | |
| 43 | + | ||
| 44 | + | ## Comments | |
| 45 | + | ||
| 46 | + | To represent a multiline text, you need to insert at the appropriate place in the string a *[[End of Line|LineEnding]]* as a word wrap signal. Example: | |
| 47 | + | ||
| 48 | + | ``` pascal | |
| 49 | + | Label1.Caption := 'This' + LineEnding + 'is' + LineEnding + 'a' + LineEnding + 'multiline' + LineEnding + 'text'; | |
| 50 | + | ``` | |
| 51 | + | ||
| 52 | + | ## See also | |
| 53 | + | ||
| 54 | + | - [[doc:/lcl/stdctrls/tlabel.html|TLabel doc]] | |
| 55 | + | - [[TStaticText]] | |
| 56 | + | ||
| 57 | + | --- | |
| 58 | + | *From the Free Pascal wiki, page "TLabel", as it stood on 2022-11-30 after a change by Ondrej, 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).* |
