?
Solved

TListItem and limitation of the caption property (string)

Posted on 2005-02-27
14
Medium Priority
?
512 Views
Last Modified: 2010-04-05
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
Comment
Question by:Nick_72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
14 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13416353
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13416360
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13416379
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Author Comment

by:Nick_72
ID: 13416411
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13416678
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
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 2000 total points
ID: 13416720
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13416724
oops forgot to say that the stringlist I created called slCaptions is a global variable
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13416746
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13416769
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
 
LVL 6

Expert Comment

by:Imthiyaz_ph
ID: 13417318
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
 
LVL 9

Author Comment

by:Nick_72
ID: 13417753
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
 
LVL 9

Author Comment

by:Nick_72
ID: 13417762
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13418747
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
 
LVL 9

Author Comment

by:Nick_72
ID: 13418829
Ok nevermind, I have implemented the Stringlist solution and it works great. :)

Nick
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question