I do this:

Label2.Font.Color := $ff0000; Label2.Width := 900; 

Blue text in 900px wide lable; (that's right.)

But it is worth doing this:

  Label2.Width := 900; Label2.Font.Color := $ff0000; 

The blue text in the lable is saved, but the width breaks down to the initial 31px (or the size of a single whole word, if it is larger than 31px)
Why is this happening?

  • 3
    I did not touch Delphi for a long time, then there seemed to be a property of the type autosize, which added up the size of the component under the insides. The change of color will lead to recalculation of the size. - KoVadim
  • @KoVadim, Thank you. And there is. So I did not touch the age of 8-10, and then decided, and I understand that the brain is no longer the same one that was 25-30 years old! He does not want to climb and pick-check all the buttons and settings, as it was before. - I_CaR

2 answers 2

TLabel has an AutoSize flag, setting which makes an automatic adjustment of the size of the control to the size of the text.

Now let's take a look: when changing the font property ( Font.Color ), it calls the Changed method which signals the owner that a change has occurred, and with such a signal about changing the font and having the AutoSize flag, the size of the control is recalculated, which resets the width to you.

Whether this behavior is correct is not quite. In theory, changing parameters that do not affect the size of the text (such as color) should not cause a recalculation of the size. And resizing the control with the AutoSize flag set should be ignored.

The solution - if you set the width yourself - set the flag Label2.AutoSize := false

    Simply, if the AutoSize = true property AutoSize = true (the default is true), then changing the content leads to a recalculation of the size and its automatic installation.

    Therefore, either use the first option, or set AutoSize = false .