Nick_72
asked on
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
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
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?
Can the string be broken up into component parts for viewing instead? Or is it meant to be one long string?
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
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
ASKER
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
>>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
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
I just tested the listview thing and yes it does seem to only allow up to 256 chars
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops forgot to say that the stringlist I created called slCaptions is a global variable
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;
// 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;
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].Sel ected then
// grab the associated string from the stringlist
MyString := Trim( slCaptions[iLoop] );
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].Sel
// grab the associated string from the stringlist
MyString := Trim( slCaptions[iLoop] );
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.
Check whether Huge String is enabled unde Project -> Options -> Compiler tab.
ASKER
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
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
ASKER
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.
I meant the TListItem class, and since TListView returns it - I guess it's hard to subclass it.
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
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
ASKER
Ok nevermind, I have implemented the Stringlist solution and it works great. :)
Nick
Nick
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.