-1

I want to add some strings to a ListView, but I dont know how long the string can be.

For example:

mystring := 'a'+|+'b'+|+ 'c'+|+ 'd'+|+ 'e'+|+ 'f'+|+ .......  '1000 of mystr';

After I explode the string, I have:

'a'
'b'
'c'
.
.
.
'1000ofmystr'

I want to add a new item to the ListView where 'A' is the Caption and 'B' and 'C' are SubItems.

Then add a new item where 'D' is the Caption and 'E' and 'F' are SubItems.

And then continue like this for the entire string, even when it has millions of substrings in the exploded data.

Every three substrings is one ListView item, until the string is exhausted.

I don't know how to do this, that's why I am asking here. What I want to do is like this picture:

image

I need something like this code:

ListView1.Items.Add;
ListView1.Caption:= StrArr[0]; // id of the book number one
SubItems.Add(StrArr[1]);   //its title 
SubItems.Add(StrArr[2]);  // its editor
ListView1.Items.Add;
ListView1.Caption:= StrArr[3]; // id of the book number two
SubItems.Add(StrArr[4]);     //its title 
SubItems.Add(StrArr[5]);    // its editor
ListView1.Items.Add;
ListView1.Caption:= StrArr[6]; // id of the book number three
// and so on for other books for an unknown number of strings

Please show me the correct code to do this.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mario
  • 1
  • 1
  • So go ahead and write the code. What is stopping you. – David Heffernan Jan 29 '17 at 20:28
  • ok i dont know hos to do that thats why i ask heer so what i want to do in a picture https://s24.postimg.org/cry3s6hs5/Captussssssssre.png then ListView1.Items.Add; ListView1.Caption:= StrArr[0]; id of the book number one SubItems.Add(StrArr[1]); //its title SubItems.Add(StrArr[2]); // its editor SubItems.Add(StrArr[3]); id of the book number tow SubItems.Add(StrArr[4]); //its title SubItems.Add(StrArr[5]); // its editor SubItems.Add(StrArr[6]); ///and else for other books UNTIL AN UNCKONOWN SubItems.Add(StrArr[100000000]); – Mario Jan 29 '17 at 21:09
  • That's not what we are here for. Read the [help] – David Heffernan Jan 29 '17 at 21:10

1 Answers1

0

Use a loop, eg:

var
  Item: TListItem:
  i: Item;

for i := 0 to NumberOfStrings-1 do
begin
  if (i mod 3) = 0 then
  begin
    Item := ListView1.Items.Add;
    Item.Caption := StrArr[i];
  end else
    Item.SubItems.Add(StrArr[i]);
end;

Do note, however, that displaying thousands/millions of items in a TListView works better if you use it in virtual mode instead (when its OwnerData property is set to true). Set its Items.Count property to the number of items to display, and then use its OnData event to provide strings for only the items it asks for, eg:

var
  NewCount: Integer;

NewCount := NumberOfStrings div 3;
if (NumberOfStrings mod 3) <> 0 then
  Inc(NewCount);
ListView1.Items.Count := NewCount;

...

procedure TMyForm.ListView1Data(Sender: TObject; Item: TListItem);
var
  Index: Integer;
begin
  Index := Item.Index * 3;
  Item.Caption := StrArr[Index];
  for Index := Index+1 to Index+2 do
  begin
    if Index < NumberOfStrings then
      Item.SubItems.Add(StrArr[Index])
    else
      Break;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770