Solved

ListVies.SaveToFile ??

Posted on 1998-08-25
6
496 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now