Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-25-2005, 06:17 AM
murphys murphys is offline
Senior Member
 
Join Date: Aug 2001
Posts: 2,948
Default listview items save to file

is there a way to save the listview items into a file?
and a fast way to restore it ?

going item by item is the only thing i can think of but mayb there is a better way?

10x
Amos
Reply With Quote
  #2  
Old 08-25-2005, 06:56 AM
douglas douglas is offline
Senior Member
 
Join Date: Jul 2001
Posts: 7,695
Default RE: listview items save to file

Its fairly easy to do if you are only saving & loading the text. If you have objects as well, then its a little more work

On the subject of saving / loading just the text...


this question has been asked many times, so i will just refer you to the links

http://www.swissdelphicenter.ch/torry/showcode.php?id=729

http://www.delphipages.com/threads/thread.cfm?ID=103562&G=103546

http://www.delphipages.com/threads/thread.cfm?ID=100043&G=100043

http://www.delphipages.com/threads/thread.cfm?ID=84084&G=84084


and many many more from this forum

http://www.delphipages.com/threads/index.cfm?LSR=1&SR=save%20listview%20items&SC=2&AO=and



Let me know if you are also saving & loading objects....



If this helps, please accept as answer!

Thanks!

UnSysApps
Reply With Quote
  #3  
Old 08-25-2005, 07:30 AM
BaraoZemo BaraoZemo is offline
Senior Member
 
Join Date: Nov 2001
Posts: 3,581
Default RE: listview items save to file

this component will use the same "technic" that unsysapps show..but is a component derived from tlistview with the save and load abilities..

after install the component in the delphi ide

[link]http://www.torry.net/vcl/lvtv/enhancedlistviews/smartlv.zip[/link]

Regards
BaraoZemo

ps, donīt forget to click in the button and Accept as Answer if this helped you!
Reply With Quote
  #4  
Old 08-25-2005, 07:55 AM
murphys murphys is offline
Senior Member
 
Join Date: Aug 2001
Posts: 2,948
Default RE: listview items save to file

it does seem to do what i want
but i do use an object.
the object is just a number (not a pointer of some kind) but it is stored in the items.object property

10x
Amos
Reply With Quote
  #5  
Old 08-25-2005, 02:14 PM
chris_w chris_w is offline
Senior Member
 
Join Date: Jan 2004
Posts: 1,397
Default RE: listview items save to file

[pre]
Code:
             procedure LVSaveToStream(LV: TListView; Stream: TStream);
             var
               i : integer;
               LI : TListItem;
               n : longint;
               smN : smallint;
               b : boolean;
               s : string;
             begin
               Stream.Size := 0;
               n := LV.Items.Count;
               Stream.Write(n, SizeOf(n));

               if n > 0 then begin
                 if LV.ItemFocused <> nil then
                   n := LV.ItemFocused.Index
                 else
                   n := -1;
                 Stream.Write(n, SizeOf(n));

                 for i := 0 to LV.Items.Count -1 do begin
                   LI := LV.Items[i];

                   s := LI.Caption;
                   smN := Length(s);
                   Stream.Write(smN, SizeOf(smN));
                   if smN > 0 then
                     Stream.Write(s[1], smN);

                   s := LI.SubItems.CommaText;
                   n := Length(s);
                   Stream.Write(n, SizeOf(n));
                   if n > 0 then
                     Stream.Write(s[1], n);

                   b := LI.Checked;
                   Stream.Write(b, SizeOf(b));

                   smN := LI.ImageIndex;
                   Stream.Write(smN, SizeOf(smN));

                   smN := LI.StateIndex;
                   Stream.Write(smN, SizeOf(smN));

                   smN := LI.OverlayIndex;
                   Stream.Write(smN, SizeOf(smN));

                   smN := LI.Indent;
                   Stream.Write(smN, SizeOf(smN));

                   //stores Data as a longint
                   n := integer(LI.Data);
                   Stream.Write(n, SizeOf(n));

                   b := LI.Selected;
                   Stream.Write(b, SizeOf(b));

                 end;
               end;
             end;

             procedure LVLoadFromStream(LV: TListView; Stream: TStream;
                                        RestoreSel: boolean = FALSE);
             var
               i : integer;
               LI : TListItem;
               cnt, n, ItemFocusedIndex : longint;
               smN : smallint;
               b : boolean;
               s : string;
             begin
               LV.Items.BeginUpdate;
               try
                 LV.Items.Clear;

                 Stream.Position := 0;

                 Stream.Read(cnt, SizeOf(cnt));
                 if cnt > 0 then begin
                   LV.AllocBy := cnt;

                   Stream.Read(ItemFocusedIndex, SizeOf(ItemFocusedIndex));

                   for i := 0 to cnt -1 do begin
                     LI := LV.Items.Add;

                     Stream.Read(smN, SizeOf(smN));
                     if smN > 0 then begin
                       SetLength(s, smN);
                       Stream.Read(s[1], smN);
                       LI.Caption := s;
                     end;

                     Stream.Read(n, SizeOf(n));
                     if n > 0 then begin
                       SetLength(s, n);
                       Stream.Read(s[1], n);
                       LI.SubItems.CommaText := s;
                     end;

                     Stream.Read(b, SizeOf(b));
                     LI.Checked := b;

                     Stream.Read(smN, SizeOf(smN));
                     LI.ImageIndex := smN;

                     Stream.Read(smN, SizeOf(smN));
                     LI.StateIndex := smN;

                     Stream.Read(smN, SizeOf(smN));
                     LI.OverlayIndex := smN;

                     Stream.Read(smN, SizeOf(smN));
                     LI.Indent := smN;

                     //sets Data to longint
                     Stream.Read(n, SizeOf(n));
                     LI.Data := TObject(n);

                     Stream.Read(b, SizeOf(b));
                     if b and RestoreSel then
                       LI.Selected := true;
                   end;
                 end;

                 if RestoreSel and
                    (ItemFocusedIndex > -1) and
                    (ItemFocusedIndex < LV.Items.Count) then
                 begin
                   LI := LV.Items[ItemFocusedIndex];
                   LI.Selected := true;
                   LI.Focused  := true;
                   LI.MakeVisible(false);
                 end;
               finally
                 LV.Items.EndUpdate;
               end;
             end;

             procedure LVSaveToFile(const Filename: string; LV: TListView);
             var
               FS : TFileStream;
             begin
               FS := TFileStream.Create(Filename, fmCreate);
               try
                 LVSaveToStream(LV, FS);
               finally
                 FS.Free;
               end;
             end;

             procedure LVLoadFromFile(const Filename: string; LV: TListView;
                                      RestoreSel: boolean = FALSE);
             var
               FS : TFileStream;
             begin
               if FileExists(Filename) then begin
                 FS := TFileStream.Create(Filename, fmOpenRead or fmShareDenyNone);
                 try
                   LVLoadFromStream(LV, FS, RestoreSel);
                 finally
                   FS.Free;
                 end;
               end;
             end;


//Example...
procedure TForm1.Button2Click(Sender: TObject);
begin
  LVSaveToFile(ChangeFileExt(ParamStr(0), '.lv'), ListView1);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  LVLoadFromFile(ChangeFileExt(ParamStr(0), '.lv'), ListView1, true);
end;
[/pre]

Chris
Reply With Quote
  #6  
Old 08-25-2005, 05:19 PM
BaraoZemo BaraoZemo is offline
Senior Member
 
Join Date: Nov 2001
Posts: 3,581
Default RE: listview items save to file

this code works very accurated for me..

and works with all kind of components ..(except containers such as TPanel, TForm, for these components , before reload you need first to clear the items inside to avoid error)

//to save
SaveFile(ChangeFileExt(ParamStr(0), '.sav'), listview1);

//to load
LoadFile(ChangeFileExt(ParamStr(0), '.sav'), listview1);

Code:
procedure SaveFile(filename: String; a: TComponent);
var
 FileStream:TFileStream;
 FileWriter:TWriter;
 BufferSize:Integer;
begin
 try
   BufferSize := 1024;
   FileStream := TFileStream.Create(filename,fmOpenWrite or fmCreate);
   FileWriter := TWriter.Create(FileStream,BufferSize);
   FileWriter.WriteRootComponent(a);
 finally
   FileWriter.Free;
   FileStream.Free;
 end;
end;

procedure LoadFile(filename: string; a: TComponent);
var
 FileStream:TFileStream;
 FileReader:TReader;
 BufferSize:Integer;
 Identification:String;
begin
 try
   BufferSize := 1024;
   FileStream := TFileStream.Create(filename,fmOpenRead);
   FileReader := TReader.Create(FileStream,BufferSize);
   FileReader.ReadRootComponent(a);
 finally
   FileReader.Free;
   FileStream.Free;
 end;
end;
[link=http://www.delphipages.com/threads/thread.cfm?ID=27230&G=27222]original post is here[/link]

Regards
BaraoZemo

ps, donīt forget to click in the button and Accept as Answer if this helped you!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 05:20 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2014, vBulletin Solutions, Inc.