delphivcl

How to make a VCL control have a fixed margin to another control?


In my form, there are two labels, the first label (Label1) is set to AutoSize=true, and its Caption may change from time to time. The second label (Label2) should have a margin 20px from Label1, even if Label1's size is changed. And the whole form should keep a 50px margin from the right of Label2.

image

I can implement this with codes:

label2.Left := label1.Left + label1.Width + 20;
Width := label2.Left + label2.Width + 50;

However, is there a way to implement without codes?


Solution

  • Use TRelativePanel as a container for the two labels and set Label2.RightOf to Label1, Label2.Margin.Left to 20 and Label2.AlignWithMargins to True