ary
ary

Reputation: 959

TLayout to AutoSize along with TLabel

I am using latest Delphi 10.1 and creating a multi-device app. I have a TLayout that has Align set to top. I have a TLabel that is with in the mentioned TLayout and has Align set to Client and its TLabel.AutoSize set to True.

Problem is that when I have long label text, TLabel grows but TLayout doesn't grow.

How can I fix this?

Upvotes: 1

Views: 1649

Answers (1)

Johan
Johan

Reputation: 76607

You know the new size of the label as soon as you change its text.
Label.Width will update to reflect the resized width.

You have 3 options.

  1. Everytime you change the text of the label, also read its width and update the associated TLayout to match (not recommended).

  2. Assign the OnResize event of the label and do something like this (recommended):

    procedure TForm45.Label1Resize(Sender: TObject);
    const
      ExtraWidthOfLayout = 10;
    var
      Lbl: TLabel;
      Layout: TLayout;
    begin
      if (Sender is TLabel) and (TLabel(Sender).Parent is TLayout) then begin
        Lbl:= TLabel(Sender);
        Layout:= TLayOut(Lbl.Parent);
        Layout.Width:= Lbl.Width + ExtraWidthOfLayout;
      end;
    end;

Note that you can use the same event for all labels.

  1. Create a new control consisting of a fused layout and Label that overrides TLabel.DoResize.

Upvotes: 1

Related Questions