-1

I have memo with text in 2 lines like

aaaa : dsfdsfdsffsf 
bbbbbbbbb : fgdfgff

I would like to get ":" in same line - one below the other like

aaaa     :dsfsfd
bbbbbbbb :ghjghjgjhjg

I use delphi and code.

memo.lines.add(some_string + ' : ' + some_string);

I tried to use SetLength but don't now how.

Michael
  • 41,989
  • 11
  • 82
  • 128
user2997342
  • 87
  • 2
  • 3
  • 8

3 Answers3

4

Set the font of the memo to a monospaced font like Courier New. That way, every character will be the same width, and you can use that to align the texts or show ASCII arts in the memo.

Alternatively you can use a listview or a stringgrid, which support displaying text in columns. For your purpose, this is probably the better option.

If you decide to stick with the memo, then apart from setting the font, you will have to make all the strings before the colons the same width. You can use a simple padding function for that. A nice example is given on SwissDelphiPages:

function RightPad(S: string; Ch: Char; Len: Integer): string;
var
  RestLen: Integer;
begin
  Result  := S;
  RestLen := Len - Length(s);
  if RestLen < 1 then Exit;
  Result := StringOfChar(Ch, RestLen) + S;
end;

So you can use it like this:

Memo.Lines.Add(RightPad('some_string', ' ', 20) + ':' + some_string);
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
3

To format the strings you can use System.SysUtils.Format

procedure Output( const AStr1, AStr2 : string; AWidth : Integer );
begin
  Writeln( Format( '%-*.*s : %s', [AWidth, AWidth, AStr1, AStr2] ) );
end;

procedure Main;
begin
  Output( 'aaaa', 'dsfdsfdsffsf', 9 );
  Output( 'bbbbbbbbb', 'fgdfgff', 9 );
end;

Output in console will be

aaaa       : dsfsfd
bbbbbbbbbb : ghjghjgjhjg

To publish this output inside a TMemo you have to select a monospaced font.

A better option is to use a grid with 2 columns.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
0

I guess you have to do iterate twice over all lines: first to know maximum length of string before the ":". Then you have to do the processing in order to get the ":" shifted individually. For this second iteration use one approach of the shown before in the other replies.

Klaus
  • 11
  • 2