2

In a Delphi 10.4.2 Win32 VCL Application on Windows 10, I use a TPanel with AutoSize = True.

The documentation says:

When AutoSize is True, the control resizes automatically when its contents change.

However, this is not the case when the size of its contents changes at run-time in this particular situation:

Place these controls on a VCL Application form:

object Button1: TButton
  Left = 40
  Top = 56
  Width = 81
  Height = 25
  Caption = 'Button1'
  TabOrder = 2
  OnClick = Button1Click
end
object Panel1: TPanel
  Left = 40
  Top = 104
  Width = 161
  Height = 19
  AutoSize = True
  TabOrder = 1
  object CheckBox1: TCheckBox
    Left = 1
    Top = 1
    Width = 159
    Height = 17
    Align = alTop
    Caption = 'bla bla bla bla bla bla'
    TabOrder = 0
    WordWrap = True
  end
end

Then, in the Button's OnClick handler, place this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Panel1.AutoSize := False;
  try
    Panel1.Width := Panel1.Width - 10;
  finally
    Panel1.AutoSize := True; // trying to refresh the Panel's height
  end;
end;

This decreases the width of the Panel by 10 pixels each time.

However, the Panel's Height is not automatically resized!

How can I manually resize the Panel, so it reflects the changed Height of the Checkbox?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 1
    The underlying problem is rather simple. To understand this, create a new VCL project and drop a `TCheckBox` on the main form. Set its `WordWrap` to `True` and its `Caption` to `'A friendly dog ate a biscuit.'`. Make the checkbox wide enough for the caption to fit on a single line. Now, in the form designer, reduce the width of the check box. You will see that, eventually, the caption wraps to two lines, but the height of the control doesn't change! So, text will be truncated. Now, in your scenario, since the check box doesn't increase its height, the panel doesn't have a new optimal height to – Andreas Rejbrand Aug 06 '21 at 10:00
  • 2
    (cont.) ...adjust to. So the actual question is "How do I auto-adjust the height of a `TCheckBox` with multiple lines of text?" – Andreas Rejbrand Aug 06 '21 at 10:01
  • Shouldn't the Checkbox automatically fit its height to its wrapped caption text? Apparently, `TCheckBox` doesn't have an `AutoSize` property? – user1580348 Aug 06 '21 at 10:08
  • No, the check box is known for not having an auto-sizing feature. This often causes problems when software is translated (say, from English to Swedish). For instance, if a check box caption initially is `'Show old messages'`, then the Swedish translation `'Visa gamla meddelanden'` may be truncated (and then you have to go back to the DFM and make the control wider). – Andreas Rejbrand Aug 06 '21 at 10:12
  • I have tried to solve the problem with an `Interposer` class that exposes the protected `AutoSize` property in `TCheckBox`. But it does not work. – user1580348 Aug 06 '21 at 10:23
  • No, the property is introduced in `TControl`, but doesn't do any auto-sizing unless code is added for that in a child class. `TCheckBox` doesn't add that code. – Andreas Rejbrand Aug 06 '21 at 10:26
  • You probably have to use `TextHeight` yourself to figure out the height. – Andreas Rejbrand Aug 06 '21 at 10:28
  • Well, you can always compute the required height yourself and then simply set the check box's height to this value. – Andreas Rejbrand Aug 06 '21 at 10:30
  • 1
    I ended up using a `TRzCheckBox` instead of a `TCheckBox`, with this code: https://i.imgur.com/tTPFVKJ.png It works perfectly now. – user1580348 Aug 06 '21 at 11:21
  • @user1580348 - Raize is a large package (and un-updated for years). And its installer is crazy. I wouldn't drag such a monster in my project just for a checkbox. I would rather extract only that control from the package. – Gabriel Mar 22 '22 at 09:17
  • @ServerOverflow IMO the RZ library is one of the best multi-purpose libraries available for Delphi developers. – user1580348 Mar 24 '22 at 13:39
  • @user1580348 - I find it buggy. – Gabriel Mar 24 '22 at 14:24

0 Answers0