AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

Txt in Listview Laden

Ein Thema von Natcree · begonnen am 16. Mär 2013 · letzter Beitrag vom 17. Mär 2013
 
Benutzerbild von Sir Rufo
Sir Rufo

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

AW: Txt in Listview Laden

  Alt 17. Mär 2013, 09:23
Zunächst würde ich alle Eigenschaften der DatenKlasse in published legen. Denn auf diese published Properties kann man sehr einfach zugreifen, ohne die Klasse genau kennen zu müssen.

Um jetzt eine beliebige TObjectList in eine Datei zu speichern oder aus ihr zu laden nimmst du einfach das hier:
Delphi-Quellcode:
unit ObjLstFiler;

interface

uses
  Contnrs;

procedure SaveList( AList : TObjectList; const AFileName : string; Delimiter : Char );
procedure LoadList( AList : TObjectList; AItemClass : TClass; const AFileName : string; Delimiter : Char );

function ObjToStr( AInstance : TObject; Delimiter : Char ) : string;
procedure ObjFromStr( AInstance : TObject; const AStr : string; Delimiter : Char );

implementation

uses
  TypInfo, Classes;

function ObjToStr( AInstance : TObject; Delimiter : Char ) : string;
var
  LPropName, LPropValue : string;
  LPropInfo : PPropInfo;
  LPropCount : Integer;
  LPropList : PPropList;
  LPropType : PPTypeInfo;
  LIdx : Integer;
  LTemp : TStrings;
begin
  LPropCount := GetPropList( AInstance, LPropList );

  LTemp := TStringList.Create;
  try

    LTemp.Delimiter := Delimiter;

    for LIdx := 0 to Pred( LPropCount ) do
      begin
        LPropInfo := LPropList^[LIdx];
        LPropType := LPropInfo^.PropType;

        LPropName := string( LPropInfo.Name );

        case LPropType^.Kind of

          tkInteger, tkChar, tkEnumeration, tkFloat, tkString, tkSet, tkWChar, tkLString, tkWString, tkInt64{$IFDEF UNICODE}, tkUString{$ENDIF} :
            begin

              // ReadOnly-Property
              if ( LPropInfo.SetProc = nil )
              then
                Continue;

              LPropValue := GetPropValue( AInstance, LPropName );

              LTemp.Add( LPropValue );

            end;

        end;
      end;

    Result := LTemp.DelimitedText;

  finally
    LTemp.Free;
  end;
end;

procedure ObjFromStr( AInstance : TObject; const AStr : string; Delimiter : Char );
var
  LPropName, LPropValue : string;
  LPropInfo : PPropInfo;
  LPropCount : Integer;
  LPropList : PPropList;
  LPropType : PPTypeInfo;
  LIdx : Integer;
  LTemp : TStringList;
  LTempIdx : Integer;
begin
  LPropCount := GetPropList( AInstance, LPropList );

  LTemp := TStringList.Create;
  try

    LTemp.Delimiter := Delimiter;
    LTemp.DelimitedText := AStr;
    LTempIdx := 0;

    for LIdx := 0 to Pred( LPropCount ) do
      begin
        LPropInfo := LPropList^[LIdx];
        LPropType := LPropInfo^.PropType;

        LPropName := string( LPropInfo.Name );

        case LPropType^.Kind of

          tkInteger, tkChar, tkEnumeration, tkFloat, tkString, tkSet, tkWChar, tkLString, tkWString, tkInt64{$IFDEF UNICODE}, tkUString{$ENDIF} :
            begin

              // ReadOnly-Property
              if ( LPropInfo.SetProc = nil )
              then
                Continue;

              LPropValue := LTemp[LTempIdx];

              Inc( LTempIdx );

              SetPropValue( AInstance, LPropName, LPropValue );

            end;

        end;
      end;

  finally
    LTemp.Free;
  end;
end;

procedure SaveList( AList : TObjectList; const AFileName : string; Delimiter : Char );
var
  LData : TStrings;
  LIdx : Integer;
begin
  LData := TStringList.Create;
  try
    for LIdx := 0 to Pred( AList.Count ) do
      LData.Add( ObjToStr( AList[LIdx], Delimiter ) );

    LData.SaveToFile( AFileName );
  finally
    LData.Free;
  end;
end;

procedure LoadList( AList : TObjectList; AItemClass : TClass; const AFileName : string; Delimiter : Char );
var
  LData : TStrings;
  LIdx : Integer;
  LObj : TObject;
begin
  AList.Clear;

  LData := TStringList.Create;
  try
    LData.LoadFromFile( AFileName );
    for LIdx := 0 to Pred( LData.Count ) do
      begin
        LObj := AItemClass.Create;
        try
          ObjFromStr( LObj, LData[LIdx], Delimiter );
          AList.Add( LObj );
          LObj := nil;
        finally
          LObj.Free;
        end;
      end;
  finally
    LData.Free;
  end;
end;

end.
Eine beispielhafte Verwendung findest du hier:
Delphi-Quellcode:
program ObjLstFile;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  SysUtils, Classes, Contnrs,
  ObjLstFiler in 'ObjLstFiler.pas';

type
  TMyObject = class
  private
    FDatum : TDateTime;
    FInfo : string;
    FKonto : Integer;
    FWert : Currency;
    FMwSt : Extended;
  published // es werden nur diese published properties gespeichert!!!
    property Datum : TDateTime read FDatum write FDatum;
    property Info : string read FInfo write FInfo;
    property Konto : Integer read FKonto write FKonto;
    property Wert : Currency read FWert write FWert;
    property MwSt : Extended read FMwSt write FMwSt;
  end;

procedure Test;
var
  LList : TObjectList;
  LItem : TMyObject;
  LIdx : Integer;
begin
  LList := TObjectList.Create( True );
  try

    // 50 Objekte erzeugen

    while LList.Count < 50 do
      begin

        LItem := TMyObject.Create;
        with LItem do
          begin
            Datum := Now;
            Info := 'Info';
            Konto := Random( 1000 ) + 1000;
            Wert := Random( 20000 ) / 100;
            MwSt := Wert * 0.19;
          end;

        LList.Add( LItem );

      end;

    // Liste speichern

    SaveList( LList, 'MyList.txt', #14 );

  finally
    LList.Free;
  end;

  LList := TObjectList.Create( True );
  try

    // Liste aus Datei laden

    LoadList( LList, TMyObject, 'MyList.txt', #14 );

    for LIdx := 0 to Pred( LList.Count ) do
      begin

        LItem := LList[LIdx] as TMyObject;

        Writeln( LItem.Datum, ' ', LItem.Info, ' ', LItem.Konto, ' ', LItem.Wert, ' ', LItem.MwSt );
      end;

  finally
    LList.Free;
  end;
end;

begin
  try

    Test;

  except
    on E : Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;

  ReadLn;

end.
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 (17. Mär 2013 um 10:33 Uhr)
  Mit Zitat antworten Zitat
 

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:47 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz