Pergunta

Quero criar uma string que abrange várias linhas para atribuir a uma propriedade de legenda de etiquetas. Como isso é feito em Delphi?

Foi útil?

Solução

No System.PAS (que é usado automaticamente), o seguinte é definido:

const
  sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF} 
               {$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF};

Isto é de Delphi 2009 (observe o uso de Ansichar e Ansistring). (Enrolamento de linhas adicionado por mim.)

Portanto, se você deseja fazer o seu embrulho tlabel, verifique se o AutoSize está definido como true e use o seguinte código:

label1.Caption := 'Line one'+sLineBreak+'Line two';

Trabalha em todas as versões de Delphi desde que o Slinebreak foi introduzido, o que acredito ser Delphi 6.

Outras dicas

Aqui está uma abordagem ainda mais curta:

my_string := 'Hello,'#13#10' world!';

my_string := 'Hello,' + #13#10 + 'world!';

#13#10 são os caracteres CR/LF em decimal

Ou você pode usar o atalho ^m+ ^j também. Tudo uma questão de preferência. Os códigos "Ctrl-Char" são traduzidos pelo compilador.

MyString := 'Hello,' + ^M + ^J + 'world!';

Você pode tirar o + entre os ^m e ^j, mas então receberá um aviso do compilador (mas ainda será compilado bem).

Por lado, um truque que pode ser útil:
Se você segurar suas múltiplas strings em um TSTRINGS, basta usar a propriedade de texto das TSTRINGS, como no exemplo a seguir.

Label1.Caption := Memo1.Lines.Text;

E você receberá seu rótulo de várias linhas ...

var
  stlst: TStringList;
begin
  Label1.Caption := 'Hello,'+sLineBreak+'world!';

  Label2.Caption := 'Hello,'#13#10'world!';

  Label3.Caption := 'Hello,' + chr(13) + chr(10) + 'world!';

  stlst := TStringList.Create;
  stlst.Add('Hello,');
  stlst.Add('world!');
  Label4.Caption := stlst.Text;

  Label5.WordWrap := True; //Multi-line Caption
  Label5.Caption := 'Hello,'^M^J'world!';

  Label6.Caption := AdjustLineBreaks('Hello,'#10'world!');
  {http://delphi.about.com/library/rtl/blrtlAdjustLineBreaks.htm}
end;

A maneira agnóstica da Plattform seria 'Slinebreak':http://www.freepascal.org/docs-html/rtl/system/slinebreak.html

Escreva ('olá' + slinebreak + 'mundo!');

ShowMessage('Hello'+Chr(10)+'World');

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.

Sometimes I don't want to clutter up my code space, especially for a static label. To just have it defined with the form, enter the label text on the form, then right click anywhere on the same form. Choose "View as Text". You will now see all of the objects as designed, but as text only. Scroll down or search for your text. When you find it, edit the caption, so it looks something like:

Caption = 'Line 1'#13'Line 2'#13'Line 3'

#13 means an ordinal 13, or ascii for carriage return. Chr(13) is the same idea, CHR() changes the number to an ordinal type.

Note that there are no semi-colon's in this particular facet of Delphi, and "=" is used rather than ":=". The text for each line is enclosed in single quotes.

Once you are done, right-click once again and choose "View as Form". You can now do any formatting such as bold, right justify, etc. You just can't re-edit the text on the form or you will lose your line breaks.

I also use "View as Text" for multiple changes where I just want to scroll through and do replacements, etc. Quick.

Dave

 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;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top