Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

TListItem and limitation of the caption property (string)

Hi,

I'm using a TListView but the problem is that some items may have a caption that is longer than 255 characters.
When I try to get the caption of a certain item, it is truncated since it returns a string, which I believe have a limit of 255 chars.

Any suggestions of what I should do?
I know I could use a memo, but I need to be able to select items, and not necessarily in order, and then process them.
Don't think I could do that with a memo (not sure though).

Thanks,

Nick
0
Nick_72
Asked:
Nick_72
  • 9
  • 4
1 Solution
 
mikelittlewoodCommented:
Are you only using the first column of a listview, or are there other items in the subitems?

You could try using a ListBox instead if you are only adding a column of items. Not sure if it is limited to 255 chars as well though.
0
 
mikelittlewoodCommented:
Is there any reason why it has to be 255 characters long?
Can the string be broken up into component parts for viewing instead? Or is it meant to be one long string?
0
 
mikelittlewoodCommented:
Or how about adding the truncated item to the listview for visual purposes only, but store each of the string captions in a stringlist.
That way when you add to a listview, add to the stringlist as well then they will be at the same reference point to process
i.e. Stringlist[0] := Listview.Items[0].Caption
Then you can multi select items in the listview and use the index of the item to reference the entire string in the stringlist.

Hope this helps
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Nick_72Author Commented:
Hi Mike,

>>Are you only using the first column of a listview

Yes, it is only one column.

>>Is there any reason why it has to be 255 characters long?

No, and not all are. But I don't set their length, just read them and present them in the listview.
They should not be broken up though.

As for the Stringlist, isn't it just a list of strings? Since the caption property of the listview is of type string, it is not the caption itself that has the limit, but the string datatype. I guess stringlist also use the string datatype which would mean the same limitation?

Thanks

Nick
0
 
mikelittlewoodCommented:
No the limit of size of data for a string is 2Gb ... i.e. VERY big  :o)

I just tested the listview thing and yes it does seem to only allow up to 256 chars

0
 
mikelittlewoodCommented:
Maybe this will help

procedure TForm1.AddString(s: String);
var
  lsti: TListItem;
begin
  // create a new listitem
  lsti := ListView1.Items.Add;
  // add the supplied string to the listview
  lsti.Caption := Trim(s);
  // now add the same string to the stringlist
  // this will add the entire string
  slCaptions.Add( Trim(s) );
end;

Function TForm1.ReturnString(Index: Integer): String;
begin
  // pass in the value of the selected item index from the listview
  // i.e. Index := Listview1.Selected.Index;
  Result := slCaptions[Index];
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // create your global stringlist
  slCaptions := TStringList.Create;
end;

procedure TForm1.FormShow(Sender: TObject);
var
  iLoop: Integer;
begin
  // loop through all the things you want to add
  For iLoop := X to Y do
    // add the string
    AddString( YourString );
end;
0
 
mikelittlewoodCommented:
oops forgot to say that the stringlist I created called slCaptions is a global variable
0
 
mikelittlewoodCommented:
All strings should be at correct positions in the stringlist and listview, but it you wanted to play it safe you could add all your strings to the stringlist first, then loop through the stringlist and add them to the listview second

// X and Y are your loop .. might replace the for loop with a query.eof if getting from a database
For iLoop := X to Y do
  slCaptions.Add( YourString );

For iLoop := 0 to Stringlist.Count - 1 do
begin
  lsti := Listview1.Items.Add
  lsti.Caption := slCaptions[iLoop]
end;
0
 
mikelittlewoodCommented:
ok I add way too many comments sometimes thinking as I go lol  :o)

You probably dont need the ReturnString function I wrote, just loop through the multi selected records of the listview

// this is the loop through the entire listview to pull back the selected records
  For iLoop := 0 to ListView1.Items.Count - 1 do
    // check if the line is selected
    if ListView1.Items[iLoop].Selected then
      // grab the associated string from the stringlist
      MyString := Trim( slCaptions[iLoop] );
0
 
Imthiyaz_phCommented:
I have used TListview and havent come across any limit of 255 chars. The problem might not be with the listview, but with the compiler setting for Huge strings. It tells the compiler whether to use AnsiString (2GB) or ShortString (255) when string type is used.

Check whether Huge String is enabled unde Project -> Options -> Compiler tab.
0
 
Nick_72Author Commented:
Yes I went reading about strings again and noticed that it can store large amount of data if the compiler option is set to Huge strings, so I went to check it out just like Imthiyaz_ph suggested, and the option is in fact checked, so this is very confusing.

However, I'm gonna go with the Stringlist solution. The only drawback I guess is if there are many items in the list, then it'll use about the double amount of memory to store them twice, but I guess I can live with that :)

One last question (although I have already accepted the answer): Is it possible to derive the TListView class and override the caption property?

I'm quite new to this, I use to program Java... :)

Thanks

Nick
0
 
Nick_72Author Commented:
Ok, it's too early in the morning... :)

I meant the TListItem class, and since TListView returns it - I guess it's hard to subclass it.
0
 
mikelittlewoodCommented:
You can override the caption property but you would have to create your own class object
Im not too sure on how to do it as I probably wouldnt do it .. hopefully someone else might be able to give you sample code.
Ive only got a general idea of how to go about it.

Mike
0
 
Nick_72Author Commented:
Ok nevermind, I have implemented the Stringlist solution and it works great. :)

Nick
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now