1

Currently, I'm setting a string which contains sLineBreak constant as value for the Caption property of a TLabel control.

MyLabel.Caption := 'A' + sLineBreak + 'B';

sLineBreak is defined in System.pas as follows (Delphi 2007):

const
      sLineBreak = {$IFDEF LINUX} #10 {$ENDIF} {$IFDEF MSWINDOWS} #13#10 {$ENDIF};

Is there a way to do the same thing by using the object inspector? (At design time).

Update: Probably in future I will move this project to a newer IDE and will develop on different platforms, but at the moment there's no particular reason why I'm using sLineBreak instead of #13#10. I'm sorry for misunderstanding.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104

3 Answers3

5

No, the Object Inspector doesn't evaluate variables at design time.

The usual way to work around this is to set the caption in the form's constructor:

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyLabel.Caption := 'A' + sLineBreak + 'B';
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
4

You cannot use the sLineBreak constant at design-time. However, you can either:

  1. Edit the DFM directly (right-click the Form Designer and choose View as Text) to insert CR/LF characters into the encoded Caption text, eg:

    Using a bare-LF line break

    object MyLabel: TLabel
      Caption = 'A'#10'B'
    end
    

    Using a CRLF line break

    object MyLabel: TLabel
      Caption = 'A'#13#10'B'
    end
    
  2. Install a third party design-time property editor (or write your own) that allows multi-line editing of String property values. For example, "Extended String Property Editor".

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The only problem with both of your proposed solutions is that it doesn't use `sLineBreak`, but hard-codes in the Windows CR/LF pair. – Ken White Sep 22 '16 at 16:08
  • @KenWhite the first solution allows you to choose which characters you want to use. The second solution uses whatever line break is used by `TStrings`, which is `sLineBreak` by default, but would be dependant on what `sLineBreak` is in the IDE's copy of the RTL, and since the IDE is a Windows app, that would be CRLF. But that is why I also mentioned writing your own property editor (which is not difficult to do), then you can use whatever characters you want. – Remy Lebeau Sep 22 '16 at 16:10
  • Editing the DFM to insert CR/LF (or simply a LF) does not handle differentiating between platforms. The property editor probably would. (I've had issues with third-party replacements of the property editor for `String` that want to do far more than desired, and to me it's not worth while to avoid adding a single line of code in the constructor). – Ken White Sep 22 '16 at 16:12
  • @KenWhite the OP is using D2007, so no cross-platform development, so `sLineBreak` is always CRLF – Remy Lebeau Sep 22 '16 at 16:15
  • Sure. Until next week when it's upgraded to Seattle or Berlin. :-) Presumably the OP has a reason for using `sLineBreak` instead of `#13#10`. – Ken White Sep 22 '16 at 16:18
  • The property editor will help with design time multi-line text entry, however it can't persist "sLineBreak" as it would prevent compilation. – Sertac Akyuz Sep 22 '16 at 17:28
  • VCL's `TLabel` accepts `bare-CR`, `bare-LF`, and `CRLF` as a line break. FireMonkey's `TLabel` accepts `bare-LF` and `CRLF`, but not `bare-CR`. So, just use `bare-LF` or `CRLF` in the DFM and you should be fine on all platforms. – Remy Lebeau Sep 22 '16 at 19:57
  • Can you show an example of ow to use `bare-LF` in a DFM file that will work? I've never seen a DFM file that will accept compile-time variables. – Ken White Sep 23 '16 at 03:37
  • @KenWhite see the example in my answer. – Remy Lebeau Sep 23 '16 at 03:40
  • Got it. You mean *hard-code the LF or CRLF in the DFM*, rather than *instead of sLineBreak you can use bare-LF or CRLF*. Misunderstood what your comment meant. – Ken White Sep 23 '16 at 03:47
3

I am assuming that you wish to do this in a cross-platform setting, in which case it cannot be done. Form files do not have any mechanism for conditional value specification. You should apply the value at runtime.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490