15 Votes

Delphi: Panel with multi-line centered Caption

Question by Guest | 2012-08-04 at 16:43

I want to write a text on a panel in a Delphi program. However, this text should be centered horizontally and vertically, and above all, it has to be multiline.

At the moment, I am trying to set the caption of the panel with the following code to a multi-line text:

Panel1.Caption := 'Line 1' + #13 + #10 + 'Line 2';

However, this does not work for some unknown reason. The new line defined with #13#10, works all the time in other contextes, but not here. What am I doing wrong?

ReplyPositiveNegative
8Best Answer10 Votes

Unfortunately, TPanel does not support multi-line captions, at least not in the newer versions of Delphi. To be able to display the caption of the panel with several lines, you have to write a custom component derived from TPanel and rewrite the OnPaint event.

But it is easier to do it like this: Simply put a TLabel on the panel, and format it as follows:

with Label1 do begin
   Align := alClient;
   Alignment := taCenter;
   AutoSize := False;
   Layout := tlCenter;
   WordWrap := True;
 
   Caption := 'Line 1' + #13 + #10 + 'Line 2';
end;

Of course, you can also use the Object Inspector to set the properties (except the #13#10).

Explanation: With "Align", the label is extended, so that it occupies the entire size of the panel. With this, however, the inscription of the label would only be seen at the top. We do not want that, so we set "Alignment" to "taCenter" and "Layout" to "tlCenter" to center the words on the label. Finally, we can still set "WordWrap" to "True", so the text on the label wraps automatically. Naturally, the caption of the panel underneath, should contain no text.
2012-08-04 at 22:14

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.