Beno
Beno

Reputation: 163

TStringGrid cannot display very long (6K) strings

I want to load some text in a TStringGrid. The strings are short except for a column where the string is over 100K. It seems that TStringGrid cannot handle this. The text does not appear in the cell until I double click the cell to edit it. But even then the behavior is erratic.

To reproduce: put a grid on the form, set goEdit= true. Run the application and double click a cell. Paste some text (should not contain enters) and press Enter to end the editing. The text disappear.

In the text I made, the limit is about 6208 ASCII chars.
Any quick fix/workaround for this?

Upvotes: 3

Views: 412

Answers (2)

Zam
Zam

Reputation: 2940

you need to use Word break. of course without Word break nothing will be displayed. and of couse your text must contain spaces.

const
  N = 16000;
var
  R: TRect;
  s: String;
  i: Integer;
begin
  R := ClientRect;
  SetLength(s, N);
  for i := 1 to N do
    if Random(10) = 0 then
      s[i] := ' '
    else
      s[i] := Char(65 + Random(26));
  Canvas.Font.Color := clBlack;

  Canvas.TextRect(R, s, [tfCenter, tfVerticalCenter, tfWordBreak]);
end;

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612993

The text is painted with ExtTextOut. That is known to fail for very long strings. For instance: ExtTextOut fails with very long strings unless lower font quality specified. From what I can tell, it is tricky to work out exactly what length of string causes failure.

I suggest that if you need to support such long strings then you draw them yourself by implementing an OnDrawCell event handler. Don't draw the entire string, because after all the user won't be able to see anything outside the cell's rectangle. That way you will be able to avoid the problem of sending ExtTextOut a string that is too long for it to handle.

Upvotes: 9

Related Questions