Thema: FreePascal Listview speichern und laden

Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#17

AW: Listview speichern und laden

  Alt 2. Nov 2014, 11:22
Du solltest dir das Leben erheblich vereinfachen indem du alles in kleine Teile aufteilst.

Hier mal ein Beispiel für deine ListView-Speicher-Problematik (in Delphi könnte in FPC etwas anders sein, das Prinzip ist aber gleich):
Delphi-Quellcode:
unit Form.Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls;

type
  TForm1 = class( TForm )
    ListView1: TListView;
    ListView2: TListView;
    SaveDialog1: TSaveDialog;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click( Sender: TObject );
    procedure Button2Click( Sender: TObject );
  private
    function Serialize( ListView: TListView ): string; overload;
    function Serialize( ListItems: TListItems ): string; overload;
    function Serialize( ListItem: TListItem ): string; overload;
    procedure Deserialize( const Data: string; ListView: TListView ); overload;
    procedure Deserialize( const Data: string; ListItems: TListItems ); overload;
    procedure Deserialize( const Data: string; ListItem: TListItem ); overload;
    procedure SaveToFile( const AFileName: string );
    procedure LoadFromFile( const AFileName: string );
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{ TForm1 }

procedure TForm1.Button2Click( Sender: TObject );
begin
  if SaveDialog1.Execute
  then
    SaveToFile( SaveDialog1.FileName );
end;

procedure TForm1.SaveToFile( const AFileName: string );
var
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    LStrings.Values['ListView1'] := Serialize( ListView1 );
    LStrings.Values['ListView2'] := Serialize( ListView2 );
    LStrings.SaveToFile( AFileName );
  finally
    LStrings.Free;
  end;
end;

function TForm1.Serialize( ListView: TListView ): string;
var
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    LStrings.Values['Items'] := Serialize( ListView.Items );
    Result := LStrings.DelimitedText;
  finally
    LStrings.Free;
  end;
end;

function TForm1.Serialize( ListItems: TListItems ): string;
var
  LIdx: Integer;
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    for LIdx := 0 to ListItems.Count - 1 do
      LStrings.Add( Serialize( ListItems[LIdx] ) );
    Result := LStrings.DelimitedText;
  finally
    LStrings.Free;
  end;
end;

function TForm1.Serialize( ListItem: TListItem ): string;
var
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    LStrings.Values['Caption'] := ListItem.Caption;
    LStrings.Values['SubItems'] := ListItem.SubItems.DelimitedText;
    Result := LStrings.DelimitedText;
  finally
    LStrings.Free;
  end;
end;

procedure TForm1.Button1Click( Sender: TObject );
begin
  if OpenDialog1.Execute
  then
    LoadFromFile( OpenDialog1.FileName );
end;

procedure TForm1.LoadFromFile( const AFileName: string );
var
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    LStrings.LoadFromFile( AFileName );
    Deserialize( LStrings.Values['ListView1'], ListView1 );
    Deserialize( LStrings.Values['ListView2'], ListView2 );
  finally
    LStrings.Free;
  end;
end;

procedure TForm1.Deserialize( const Data: string; ListView: TListView );
var
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    LStrings.DelimitedText := Data;
    Deserialize( LStrings.Values['Items'], ListView.Items );
  finally
    LStrings.Free;
  end;
end;

procedure TForm1.Deserialize( const Data: string; ListItems: TListItems );
var
  LIdx: Integer;
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    LStrings.DelimitedText := Data;
    ListItems.Clear;
    for LIdx := 0 to LStrings.Count - 1 do
      Deserialize( LStrings[LIdx], ListItems.Add );
  finally
    LStrings.Free;
  end;
end;

procedure TForm1.Deserialize( const Data: string; ListItem: TListItem );
var
  LStrings: TStrings;
begin
  LStrings := TStringList.Create;
  try
    LStrings.DelimitedText := Data;
    ListItem.Caption := LStrings.Values['Caption'];
    ListItem.SubItems.DelimitedText := LStrings.Values['SubItems'];
  finally
    LStrings.Free;
  end;
end;

end.
Alles nur kleine Methoden ... hübsch, gell?

UPDATE Kleine Verbesserung für zuküntige Erweiterung
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo ( 2. Nov 2014 um 11:37 Uhr)
  Mit Zitat antworten Zitat