Solved

ListVies.SaveToFile ??

Posted on 1998-08-25
6
515 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
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.

 

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

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!

Question has a verified solution.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month4 days, 6 hours left to enroll

630 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