Solved

Saving ListView Items to String (and Restoring ListView Items from String) ????

Posted on 2008-10-25
8
645 Views
Last Modified: 2011-10-19
I allow my users to build a filter string, from a form. They seelct combobox items and enter values in edit boxes, and click the add button to add the filter criteria to a ListView control (3 columns).

Field, Operator, Value

Example:  Year = 2008

The problem is, that the third colum could have values which include a space in the string

example:  Finish = Antique Silver

When there is a space, things get truncated and i loose half of the string

see code snipplets
function ListViewToFilter(LV: TListView): String;
var
  Items: TStringList;
  I: Integer;
  Part1, Part2, Part3: String;
begin
  Items := TStringList.Create;
  for I := 0 to LV.Items.Count - 1 do
  begin
   Part1:= LV.Items[I].Caption;
   Part2:= LV.Items[I].SubItems[0];
   Part3:= LV.Items[I].SubItems[1];
   if Part3 = '' then Part3:= QuotedStr(Part3);
   Items.Add(Part1 +',' + Part2 + ',' + Part3);
  end;
  result:= Items.Text;
end;
 
procedure FilterToListView(FStr: String; LV: TListView);
var
 SL, RL: TStringList;
 LI: TListItem;
 I: Integer;
begin
 SL:= TStringList.Create;
 SL.Text:= Copy(FStr, 2, pos(']', FStr)-2);
 LV.Items.Clear;
 for I:= 0 to SL.Count-1 do
 begin
  LI:= LV.Items.Add;
  RL:= TSTringList.Create;
  RL.CommaText:= SL[I];
  LI.Caption:= RL[0];
  LI.SubItems.Add(RL[1]);
  LI.SubItems.Add(RL[2]);
  RL.Free;
 end;
 SL.Free;
end;

Open in new window

0
Comment
Question by:geocoins-software
[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
  • 6
8 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 22805672
That is because you set the value sl.text, it sees the space as a new word and adds a second entry for it.
0
 

Author Comment

by:geocoins-software
ID: 22805688
yes, i figured it was that - and i tried CommaText, but it doesn't work either.

Do you have a fix?
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 22806130
use Delimited Text and AnsiDequotedStr
and use 2 StringLists to inflate or deflate
function ListViewToFilter(LV: TListView): String;
var
  Filter: Items: TStringList;
  I: Integer;
begin
  Filter := TStringList.Create;
  try
    Items := TStringList.Create;
    try
      Items.Delimiter := ';';
      for I := 0 to LV.Items.Count - 1 do
      begin
        Items.Clear;
        Items.Add(QuotedStr(LV.Items[I].Caption)));
        Items.Add(QuotedStr(LV.Items[I].SubItems[0]));
        Items.Add(QuotedStr(LV.Items[I].SubItems[1]));
        Filter.Add(Items.DelimitedText);
      end;
    finally
      FreeAndNil(Items);
    end;
    result:= Filter.Text;
  finally
    FreeAndNil(Filter);
  end;
end;
 
procedure FilterToListView(FStr: String; LV: TListView);
var
 SL, RL: TStringList;
 LI: TListItem;
 I: Integer;
begin
  LV.Items.Clear; 
  SL:= TStringList.Create;
  try
    SL.Text := FStr; 
    RL:= TSTringList.Create;
    try
      RL.Delimiter := ';';
      for I:= 0 to SL.Count-1 do
      begin
        RL.DelimitedText := SL[I];
        LI:= LV.Items.Add;
        LI.Caption:= AnsiDequotedStr(RL[0]);
        LI.SubItems.Add(AnsiDequotedStr(RL[1]));
        LI.SubItems.Add(AnsiDequotedStr(RL[2]));
      end;
    finally
      FreeAndNil(RL);
    end;
  finally
    FreeAndNil(SL);
  end; 
end;

Open in new window

0
Independent Software Vendors: 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!

 

Author Comment

by:geocoins-software
ID: 22807366
Geert_Gruwez:

I have posted screenshots of your code in action

Screenshot 1 is what the filter string would look like stored in the registry after using the

ListViewToFilter

Screenshot2 is what the ListView would look like after using the

FilterToListView

thanks



screenshot1.jpg
screenshot2.jpg
0
 

Author Comment

by:geocoins-software
ID: 22807426
Should look like this
screenshot3.jpg
0
 

Author Comment

by:geocoins-software
ID: 22807464
Geert_Gruwez:

Disregard my last two comments. I found a bug on my side....i will get back to you

0
 

Author Closing Comment

by:geocoins-software
ID: 31510028
GREAT!  Thanks.
0
 

Author Comment

by:geocoins-software
ID: 22807539
I am creating another question related
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 316
Delphi selector screen 2 92
Performance of SQL statement 37 137
IP without any Dots 1 61
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

697 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