Registriert seit: 12. Jun 2003
107 Beiträge
Delphi 7 Enterprise
|
Re: ListView laden
14. Dez 2007, 00:27
Das dürfte hierzu passen, es gibt andere Lösungen, diese kann u.a. auch <space> richtig schreiben.
Als Delimiter habe ich das Zeichen chr(254) genommen.
Die Konstanten
WM_LVF_ERROR, WM_LVF_COUNT, WM_LVF_END, kann man weglassen, wenn man keine Fehlermeldungen usw. haben möchte.
Code:
uses
SysUtils,System,Classes
const
WM_LVF_ERROR = WM_USER + 10;
WM_LVF_COUNT = WM_LVF_ERROR + 1;
WM_LVF_END = WM_LVF_COUNT + 1;
C_LV_Delimiter = chr(254);
//------------------------------------------------------------------------------
procedure _Explode(TS:TStrings; Sub:string; const S: string);
var//uses SysUtils,System,Classes
S2: string;
I: integer;
begin
I := 0;
S2 := S;
TS.Clear;
S2 := trim(S);
if S2 = '' then exit;
if S2 = Sub then exit;
S2 := S2 + Sub;
{-$R}
repeat
TS.Add(Copy(S2, 0,Pos(Sub, S2) - 1));
delete(S2, 1,length(TS[I] + Sub));
inc(I);
until S2 = '';
{+$R}
end;
//------------------------------------------------------------------------------
procedure TMain.SaveToFile(const DateiName: string);
var
TS : TStringList;
I,J : integer;
S : string;
begin
try
TS:= TStringList.Create;
for I := 0 to (ListView.Items.Count - 1) do
begin
S:=IntToStr(ListView.Items.Item[I].ImageIndex)+C_LV_Delimiter+
ListView.Items.Item[I].Caption+C_LV_Delimiter;
for J := 0 to Items[I].SubItems.Count -1 do
S:= S+ ListView.Items.Item[I].SubItems[J]+C_LV_Delimiter;
S:= Copy(S,1,length(S)-1);
TS.Add(S);
end;
TS.SaveToFile(DateiName);
finally
FreeAndNil(TS);
end;
end;
//------------------------------------------------------------------------------
procedure TMain.LoadFromFile(const DateiName: string);
var
TS1,TS2 : TStringList;
ListItem : TListItem;
I,J : integer;
begin
if not FileExists(DateiName) then
begin
//SendMessage(AHandle,WM_LVF_ERROR,longInt(@Dateiname),1);//Error 1 = keine Datei vorhanden
Exit;
end;
Self.Clear;
TS1:= TStringList.Create;
TS2:= TStringList.Create;
try
TS1.LoadFromFile(DateiName);
for I:=0 to TS1.Count-1 do
begin
_Explode(TS2,C_LV_Delimiter,TS1[I]);
ListItem := ListView.Items.Add;
ListItem.ImageIndex:=StrToInt(TS2.Strings[0]);
ListItem.Caption :=TS2.Strings[1];
for J:=2 to TS2.Count-1 do ListItem.SubItems.Add(TS2.Strings[J]);
//SendMessage(AHandle,WM_LVF_COUNT,0,I);
end;
finally
FreeAndNil(TS1);
FreeAndNil(TS2);
//SendMessage(AHandle,WM_LVF_END,0,0);
end;
end;
|
|
Zitat
|