1

If I put tab characters into a string and then assign the Caption property of a label from the string what do the tab characters do?

CR characters cause a return, which is useful for displaying a multi-line caption. Tab characters do seem to perform some sort of tabbing action - I'm wondering if this behaviour is defined or predictable. (I guess the behaviour is determined by Windows, not Delphi).

rossmcm
  • 5,493
  • 10
  • 55
  • 118
  • My guess is that they expand into 8 spaces. :-) – Warren P Oct 25 '11 at 03:00
  • TCustomLabel.Paint eventually calls DrawText (http://msdn.microsoft.com/en-us/library/dd162498%28v=vs.85%29.aspx) with DT_EXPANDTABS set (unless using a Ellipsis position other than epNone), so yes, it is determined by Windows – Gerry Coll Oct 25 '11 at 03:26

1 Answers1

4

If you put tab characters in a TLabel.Caption, the Caption contains tab characters.

How the tab character is displayed depends on the font you use and Windows itself. A quick test in XE, for instance, on Win7 displays spacing appropriate for tab characters (approximately 8 spaces, in a non-proportional font).

Here's my test. Drop three labels on a form, and add this to the form's OnCreate event:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label2.Left := Label1.Left;
  Label3.Left := Label2.Left;
  Label1.Caption := 'Some text'#9'Some text'#9'More text';
  Label2.Caption := Label1.Caption;
  Label3.Caption := Label1.Caption;
end;

Here's the output:

Sample label output

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Thanks @Ken. My tests also indicate that it behaves relatively sensibly for proportional fonts also, but if I want to maintain the stuff in column 2 all tabbed sensibly, I need to pad out the strings in column 1 with spaces so that they are roughly the same width or items will jump back to the previous tab position (or I guess you could use Canvas.TextWidth) – rossmcm Oct 25 '11 at 03:39