Question

I need to add 600+ (or more) characters on Item.Caption and SubItems, but I see that TListView cuts completely the text if it is longer than N characters.

I tried this:

procedure TForm1.FormCreate(Sender: TObject);
var
 i1: Integer;
 s: String;
begin
 for i1 := 0 to 690 do
  s := s + IntToStr(i1) + '-';

 with ListView1.Items.Add do
 begin
   Caption := s;
   SubItems.Add(s);
 end;
end;

And then I enabled the ListView1.OwnerDraw := True;

As you can see from the picture below, the text from Column1 goes over Column2:

enter image description here

Anyone can help me in fixing this issue ?

Was it helpful?

Solution

A quick test application in Delphi 2007, using the following (more reasonable) code, shows that the ListView stops displaying Ansi characters at 88-8, which is a length of 259 characters.

procedure TForm4.FormCreate(Sender: TObject);
var
  s: string;
  i: Integer;
  Item: TListItem;
begin
  s := '';
  for i := 0 to 89 do
    s := s + '-' + IntToStr(i);

  // Set the width of the first column so there's room for all
  ListView1.Columns[0].Width := ListView1.Canvas.TextWidth(s) + 10;

  Item := ListView1.Items.Add;
  Item.Caption := s;
  Item.SubItems.Add(s);

  // Display length of string actually displayed, which
  // is one short of the total length (the final '9' in '89'
  // is truncated), in the form's caption.
  Caption := IntToStr(Length(s) - 1);
end;

Adding a null terminator to that (as required by the Windows API) means that it's 260 characters, which according to MSDN documentation is the maximum length of the displayed text; the LVITEM.pszText member can store more, but it won't display it.

(Thanks to @SertacAkyuz for the link so I didn't have to find it.)

You can verify this yourself using RegEdit. Find a registry value that exceeds that limit (I quickly found HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Arbiters\AllocationOrder, for instance). RegEdit truncates the display no matter how wide you drag the Data column, but will show you the full text in a single line hint if you hover the mouse over it. (Of course, it's impossible to read it all unless you have multiple wide monitors, because you can't scroll the hint window.)

It's impossible to say what's wrong with your OwnerDraw code (if you have any), because you didn't post it. Merely setting OwnerDraw := True; doesn't do anything without providing an event to do the drawing.

Just as a comment: I'd rethink your design if I were you. This is terrible from a UI standpoint, and I can demonstrate why. Change the code above to your original 690 value and run the code. You'll see that the first column indeed sets it's width to enough to display all, even though the text stops at the same point (88-8). However, notice how far you have to keep scrolling to find column two? If I was using your software, that would stink.

It would be much better, IMO, to display a small amount of the text in the Caption, and display the full text in a label or memo control if the user clicks on it to indicate they actually want to read it all, or display it in a pop-up window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top