Solved

ListVies.SaveToFile ??

Posted on 1998-08-25
6
509 Views
Last Modified: 2012-06-22
ListVies.SaveToFile ??

Hi there, I need to save the contents of a ListView with multiply columns, I have thought of
saving each column by coping the contents of a column to a TStringList and then save in a new file, so the file would look something like this

[Column1]
.
.
.
.


[Column2]
.
.
.
.


The file gets rather big, so isn't there a better way to do that?
0
Comment
Question by:koger
6 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1337652
0
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1337653
Hi,
Here's a very simple solution, using TStringList.CommaText:

procedure TForm1.Button1Click(Sender: TObject); // save
var
  List1, List2: TStringList;
  iRow, iCol  : Integer;
begin
  List1 := TStringList.Create;
  List2 := TStringList.Create;
  for iRow := 0 to ListView1.Items.Count - 1 do
  with ListView1.Items[ iRow ] do begin
    List1.Add( Caption );
    for iCol := 0 to SubItems.Count - 1 do List1.Add( SubItems[ iCol ] );
    List2.Add( List1.CommaText );
    List1.Clear;
  end;
  List2.SaveToFile( 'c:\listviewcontents.dat' );
  List1.Free;
  List2.Free;
  ListView1.Items.Clear; // not necessary except for this example...
end;

procedure TForm1.Button2Click(Sender: TObject); // load
var
  List1, List2: TStringList;
  iRow, iCol  : Integer;
begin
  List1 := TStringList.Create;
  List2 := TStringList.Create;
  List2.LoadFromFile( 'c:\listviewcontents.dat' );
  ListView1.Items.BeginUpdate;
  for iRow := 0 to List2.Count - 1 do
  with ListView1.Items.Add do begin
    List1.CommaText := List2[ iRow ];
    if List1.Count > 0 then begin
      Caption := List1[ 0 ];
      for iCol := 1 to List1.Count - 1 do SubItems.Add( List1[ iCol ] );
    end;
  end;
  List1.Free;
  List2.Free;
  ListView1.Items.EndUpdate;
end;

/// John
0
 

Expert Comment

by:lavar
ID: 1337654
var
  Stream: TFileStream;
begin
{++Save ListView items++}
Stream := TFileStream.Create('ListView1.dat', fmCreate);
Stream.WriteComponentRes('Items', ListView1);
Stream.Destroy;

{++Load ListView items++}
Stream := TFileStream.Create('ListView1.dat', fmOpenRead);
Stream.ReadComponentRes(ListView1);
Stream.Destroy;
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!

 

Author Comment

by:koger
ID: 1337655
Very good answer

The only problem is that the code is very very slow, it can take more than ten minuttes to load a file with about 1000 columns and 8 rows, the problem is not your code, but TListView's add procedure it's very slow I have tried using AllocBy, but it didn't help.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1337656
Hi,
I think there are some speed improved listviews on DSP or Torry's.
Look them up, I hope they might speed up things for you...
You can also use beginupdate/endupdate on the TStringLists to speed up things some.

/// John
0
 

Expert Comment

by:menorcanet
ID: 23252367
lavar's code works really well, just what I was looking for. thanks!
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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