Solved

ListVies.SaveToFile ??

Posted on 1998-08-25
6
512 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
[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 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
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!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
Convert a string into a TDateTime 5 81
control image tags in a string ? 12 171
Unique identifier on a terminal server (rdp) 4 86
DBCtrlGrid, Delphi, Scroll 8 88
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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