I want to create a string that spans multiple lines to assign to a Label Caption property. How is this done in Delphi?
I want to create a string that spans multiple lines to assign to a Label Caption property. How is this done in Delphi?
In the System.pas (which automatically gets used) the following is defined:
const
sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF}
{$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF};
This is from Delphi 2009 (notice the use of AnsiChar and AnsiString). (Line wrap added by me.)
So if you want to make your TLabel wrap, make sure AutoSize is set to true, and then use the following code:
label1.Caption := 'Line one'+sLineBreak+'Line two';
Works in all versions of Delphi since sLineBreak was introduced, which I believe was Delphi 6.
Or you can use the ^M+^J shortcut also. All a matter of preference. the "CTRL-CHAR" codes are translated by the compiler.
MyString := 'Hello,' + ^M + ^J + 'world!';
You can take the + away between the ^M and ^J, but then you will get a warning by the compiler (but it will still compile fine).
On the side, a trick that can be useful:
If you hold your multiple strings in a TStrings, you just have to use the Text property of the TStrings like in the following example.
Label1.Caption := Memo1.Lines.Text;
And you'll get your multi-line label...
You have the const sLineBreak in the System.pas unit that already does the treatment according to the OS you are working on.
Example of use:
TForm1.btnInfoClick(Sender: TObject);
begin
ShowMessage ('My name is Jhon' + sLineBreak
'Profession: Hollywood actor');
end;
I dont have a copy of Delphi to hand, but I'm fairly certain if you set the wordwrap property to true and the autosize property to false it should wrap any text you put it at the size you make the label. If you want to line break in a certain place then it might work if you set the above settings and paste from a text editor.
Hope this helps.
private
{ Private declarations }
{declare a variable like this}
NewLine : string; // ok
// in next event handler assign a value to that variable (NewLine)
// like the code down
procedure TMainForm.FormCreate(Sender: TObject);
begin`enter code here`
NewLine := #10;
{Next Code To show NewLine In action}
//ShowMessage('Hello to programming with Delphi' + NewLine + 'Print New Lin now !!!!');
end;