Size a form to match the child THTMLabel's Autosized Height.

I have a form consisting of a button bar (currently bottom aligned) and a THTMLabel (currently Top aligned).

I'm trying to fill the HTMLLabel with content, then Autosize it - then adjust the height of the form to match the content.

My current code is:

procedure TForm1.OKButtonClick(Sender: TObject);
const
Base='Charge $9.00 to\[Welcome Drink - HKI''s] Account?';
var
I:integer;
M:string;
begin
Height:=450;
M:='';
for I := 1 to string(LinesCOunt.Text).ToInteger do
M:=M+Base+'\';
MessageLabel.HTMLText.Text:=M.Replace('\','
');
MessageLabel.Doit;
ClientHeight:=Self.MessageLabel.ClientHeight+Self.ButtonPanel.Height+10;
end;

However I just can't get this to work reliably.

What is the correct approach ?

I tried to setup something according to your description and this appears to work.
The test project is attached.
Project1.zip (4.8 KB)

The problem I have is that the 'DoIt' procedure sometimes doesn't do it.

I think I may have found the 'correct' way to do it...

procedure TGenericQuestionForm.MessageLabelNewSize(Sender: TObject; NewWidth,
NewHeight: Integer);
begin
inherited;
ClientHeight:=NewHeight+ButtonPanel.Height+10;
end;

then

MessageLabel.AutoSizing:=true;
MessageLabel.AutoSizeType:=asVertical;
MessageLabel.HTMLText.Text:=Whirlcode(Caption,taCenter,'#FF0000','#0000FF');

1 Like