55 Votes

Delphi/Lazarus: Dynamically create a Label at runtime

Tip by Stefan Trost | Last update on 2022-11-18 | Created on 2013-10-23

With this little code example, I want to show you how you can create and place a new TLabel dynamically at runtime of your program.

Let us first look at the code that can be used for Delphi and Lazarus in the same way:

var
  ANewLabel: TLabel;
begin
  ANewLabel         := TLabel.Create(Form1);
  ANewLabel.Name    := 'la_new';
  ANewLabel.Caption := 'New Label';
  ANewLabel.Left    := 20;
  ANewLabel.Top     := 10;
  ANewLabel.Visible := true;
  ANewLabel.Parent  := Form1;
end;

With TLabel.Create(), we can put a new Label out of nowhere. So that we can change its properties, we use the variable "ANewLabel" in which we temporarily store a reference to the new label, so that we can then use this variable to set some properties of the label.

Name, Parent and Owner of the Label

Above all, two things are important in the code: First, each control on the same form has to have a unique name. If we set the name of the new Label to "la_new" like in our example and we would like to create another Label after that, we have to use another name for this (for example "la_new2").

Secondly, it is very important that we set the property "Parent" of the Label. This determines on which other control the label is set and becomes visible there. In the example, we place the Label on "Form1" (the name of our form), but we can also use a Panel (TPanel) or a TabSheet (TTabSheet) or whatever else. If we would not set the property "Parent", our Label would remain invisibly hidden and would only exist in the memory.

If we do not want to take care of releasing the memory of the Label again, after the Label is no longer needed, it is similarly important to set the "Owner" of the Label. However, this time, we do not subsequently set this via one of the properties of the Label but directly when creating the Label as a parameter of Create() by passing "Form1" here. As a result, the Label is automatically released when our Form is released and we save ourselves to call the .Free procedure of the Label manually.

Positioning and Caption

Apart from that, we set some others of the properties, such as "Left" and "Right" to position the label on the form (here 20 pixels from the left border and 10 pixels from the upper border) or "Caption" to display a text on the label.

Of course, in this way we could also set all conceivable other properties that are otherwise adjustable via the Object Inspector.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.