TLabel
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
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.
Usage
A TLabel is one of the most basic components that can be used on forms. Most labels are the marking of other components, such as Edit fields, Memos, StringGrids and so on. TLabels provide a variety of events available to you but not required in most cases.
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.
To change the default label Label of a new inserted TLabel on a form, you can proceed as follows:
- On your form with one click, select the TLabel.
- Go on properties in the Object Inspector tab.
- Select the property Caption and change it in the next edit field.
- In the same way, you can select the property Name and give the label a better name.
Of course, you can change the caption (the text displayed) during run time.
The following example demonstrates this:
- Create a new 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).
- Create now a event handler for Button1, by simply double clicking on Button1.
- Insert following lines of code in the OnClick event handler of Button1:
procedure TForm1.Button1Click(Sender: TObject);
const Cnt: Integer = 0; //Counter to determine how many times the button has been clicked
begin
inc(Cnt); //Increment the counter by 1
Label1.Caption:='Button was clicked ' + //Write the text on the caption of Label1
IntToStr(Cnt) + ' times';
end;
- Start your program and test the change of the label caption by clicking the button.
Delphi incompatibility for right-aligned labels
- 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.
- 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.
- 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.
Comments
To represent a multiline text, you need to insert at the appropriate place in the string a LineEnding as a word wrap signal. Example:
Label1.Caption := 'This' + LineEnding + 'is' + LineEnding + 'a' + LineEnding + 'multiline' + LineEnding + 'text';
See also