Delphi 简单TListView保存并加载到文件和从文件加载(仅保存列字符串值)

Delphi 简单TListView保存并加载到文件和从文件加载(仅保存列字符串值),delphi,delphi-2010,Delphi,Delphi 2010,我正在使用Delphi2010,我在互联网上搜索并找到了一些示例,但它们都不起作用。我使用它可能是因为2010和unicode?反正 我正在寻找两个例程来为TListView进行简单的文件保存和加载。 我只对保存每列中的字符串值感兴趣。i、 e.标题和子项。我对保存布局或任何对象不感兴趣 procedure SaveToFile(const FileName: string); procedure LoadFromFile(const FileName: string); 这里有一些非常粗糙的

我正在使用Delphi2010,我在互联网上搜索并找到了一些示例,但它们都不起作用。我使用它可能是因为2010和unicode?反正

我正在寻找两个例程来为TListView进行简单的文件保存和加载。 我只对保存每列中的字符串值感兴趣。i、 e.标题和子项。我对保存布局或任何对象不感兴趣

procedure SaveToFile(const FileName: string);
procedure LoadFromFile(const FileName: string);

这里有一些非常粗糙的东西。它使用相当有限的制表符分隔文本格式。内容不允许包含内联制表符。我还对load函数实现了任何错误检查。我相信你可以补充一点

uses
  ComCtrls, Types, StrUtils;

procedure ListViewSaveToFile(ListView: TListView; const FileName: string);

  procedure AddTextToLine(var Line: string; const Text: string);
  begin
    Line := Line + Text + #9;
  end;

  procedure MoveCompletedLineToList(const Strings: TStringList; var Line: string);
  begin
    Strings.Add(System.Copy(Line, 1, Length(Line)-1));//remove trailing tab
    Line := '';
  end;

var
  Strings: TStringList;
  LatestLine: string;
  i, j: Integer;

begin
  LatestLine := '';

  Strings := TStringList.Create;
  try
    for i := 0 to ListView.Items.Count-1 do begin
      AddTextToLine(LatestLine, ListView.Items[i].Caption);
      for j := 0 to ListView.Items[i].SubItems.Count-1 do begin
        AddTextToLine(LatestLine, ListView.Items[i].SubItems[j]);
      end;
      MoveCompletedLineToList(Strings, LatestLine);
    end;
    Strings.SaveToFile(FileName, TEncoding.UTF8);
  finally
    Strings.Free;
  end;
end;

procedure ListViewLoadFromFile(ListView: TListView; const FileName: string);
var
  Strings: TStringList;
  i, j: Integer;
  Fields: TStringDynArray;
  Item: TListItem;
begin
  Strings := TStringList.Create;
  try
    Strings.LoadFromFile(FileName);
    ListView.Clear;
    for i := 0 to Strings.Count-1 do begin
      Fields := SplitString(Strings[i], #9);
      Item := ListView.Items.Add;
      Item.Caption := Fields[0];
      for j := 1 to high(Fields) do begin
        Item.SubItems.Add(Fields[j]);
      end;
    end;
  finally
    Strings.Free;
  end;
end;

这里有一些非常粗糙的东西。它使用相当有限的制表符分隔文本格式。内容不允许包含内联制表符。我还对load函数实现了任何错误检查。我相信你可以补充一点

uses
  ComCtrls, Types, StrUtils;

procedure ListViewSaveToFile(ListView: TListView; const FileName: string);

  procedure AddTextToLine(var Line: string; const Text: string);
  begin
    Line := Line + Text + #9;
  end;

  procedure MoveCompletedLineToList(const Strings: TStringList; var Line: string);
  begin
    Strings.Add(System.Copy(Line, 1, Length(Line)-1));//remove trailing tab
    Line := '';
  end;

var
  Strings: TStringList;
  LatestLine: string;
  i, j: Integer;

begin
  LatestLine := '';

  Strings := TStringList.Create;
  try
    for i := 0 to ListView.Items.Count-1 do begin
      AddTextToLine(LatestLine, ListView.Items[i].Caption);
      for j := 0 to ListView.Items[i].SubItems.Count-1 do begin
        AddTextToLine(LatestLine, ListView.Items[i].SubItems[j]);
      end;
      MoveCompletedLineToList(Strings, LatestLine);
    end;
    Strings.SaveToFile(FileName, TEncoding.UTF8);
  finally
    Strings.Free;
  end;
end;

procedure ListViewLoadFromFile(ListView: TListView; const FileName: string);
var
  Strings: TStringList;
  i, j: Integer;
  Fields: TStringDynArray;
  Item: TListItem;
begin
  Strings := TStringList.Create;
  try
    Strings.LoadFromFile(FileName);
    ListView.Clear;
    for i := 0 to Strings.Count-1 do begin
      Fields := SplitString(Strings[i], #9);
      Item := ListView.Items.Add;
      Item.Caption := Fields[0];
      for j := 1 to high(Fields) do begin
        Item.SubItems.Add(Fields[j]);
      end;
    end;
  finally
    Strings.Free;
  end;
end;

我提出了自己的解决方案,使用了TStringList和名称/值对,看起来效果不错。我将项目标题存储在名称中,并将所有子列存储在值中,然后只解析各个子项目的值

procedure LoadFromFile(AListView: TListView; AFileName: string);
var
 I : Integer;
 SL: TStringList;
 Item: TListItem;
 Col0, Col1, Col2, Col3, Col4, Col5: String;
begin
  SL:= TStringList.Create;
  try
    SL.LoadFromFile(AFileName);
    AListView.Items.BeginUpdate;
    AListView.Items.Clear;
    for I:=  0 to SL.Count - 1 do
    begin
     Item:= AlistView.Items.Add;
     Item.Caption:= SL.Names[I];
     parseValue(SL.ValueFromIndex[I], Col0, Col1, Col2, Col3, Col4, Col5);
     Item.SubItems.Add(Col0);
     Item.SubItems.Add(Col1);
     Item.SubItems.Add(Col2);
     Item.SubItems.Add(Col3);
     Item.SubItems.Add(Col4);
     Item.SubItems.Add(Col5);
    end;
    AListView.Items.EndUpdate;
  finally
   SL.Free;
  end;
end;

    procedure SaveToFile(AListView: TListView; AFileName: string);
    var
     I: Integer;
     SL: TStringList;
    begin
     SL:= TStringList.Create;
     for I := 0 to AListView.Items.Count - 1 do
     begin
      SL.Add(AlistView.Items[I].Caption + '=' +
             AlistView.Items[I].SubItems[0] + ',' +
             AlistView.Items[I].SubItems[1] + ',' +
             AlistView.Items[I].SubItems[2] + ',' +
             AlistView.Items[I].SubItems[3] + ',' +
             AlistView.Items[I].SubItems[4] + ',' +
             AlistView.Items[I].SubItems[5])
     end;
     try
      SL.SaveToFile(AFileName);
     finally
       SL.Free;
     end;
    end;

 procedure parseValue(AValue: String; var Col0, Col1, Col2, Col3, Col4, Col5: String);
 var
  I: Integer;
  S: String;
  L: TStringList;
 begin
  //create a temporary list to store the data parts in
  L:= TStringList.Create;
  try
  //iterate through the length of the string
  for I:= 1 to length(AValue) do
  begin
   //if the char is not a comma, append it to S
   if AValue[I] <> ',' then
    S:= S + AValue[I]
   else
   // if char is a comma, add S to temporary list and reset S
   begin
    L.Add(S);
    S:= '';
   end;
  end;
  //add the last string to temporary list
  L.Add(S);
  //assign the items in temporary list to variables to be passed back 
  Col0:= L[0];
  Col1:= L[1];
  Col2:= L[2];
  Col3:= L[3];
  Col4:= L[4];
  Col5:= L[5];
  finally
   L.Free;
  end;
 end;
过程LoadFromFile(AListView:TListView;AFileName:string);
变量
I:整数;
SL:TStringList;
项目:技术指标;
Col0,Col1,Col2,Col3,Col4,Col5:字符串;
开始
SL:=TStringList.Create;
尝试
SL.LoadFromFile(文件名);
AListView.Items.BeginUpdate;
AListView.Items.Clear;
对于I:=0到SL。计数-1 do
开始
项目:=AlistView.Items.Add;
项目名称:=SL.Names[I];
parseValue(SL.ValueFromIndex[I],Col0,Col1,Col2,Col3,Col4,Col5);
项目.子项目.添加(Col0);
项目.子项目.添加(第1栏);
项目.子项目.添加(Col2);
项目.子项目.添加(第3栏);
项目.子项目.添加(第4栏);
项目.子项目.添加(第5栏);
终止
AListView.Items.EndUpdate;
最后
SL.免费;
终止
终止
过程SaveToFile(AListView:TListView;AFileName:string);
变量
I:整数;
SL:TStringList;
开始
SL:=TStringList.Create;
对于I:=0到AListView.Items.Count-1 do
开始
SL.Add(AlistView.Items[I].标题+'='+
AlistView.Items[I]。子项[0]+,'+
AlistView.Items[I]。子项[1]+,'+
AlistView.Items[I].子项[2]+','+
AlistView.Items[I].子项[3]+','+
AlistView.Items[I].子项[4]+','+
AlistView.Items[I].子项[5])
终止
尝试
SL.SaveToFile(文件名);
最后
SL.免费;
终止
终止
过程解析值(AValue:String;var Col0、Col1、Col2、Col3、Col4、Col5:String);
变量
I:整数;
S:字符串;
L:TStringList;
开始
//创建临时列表以存储数据部分
L:=TStringList.Create;
尝试
//遍历字符串的长度
对于I:=1到长度(AValue)do
开始
//如果字符不是逗号,请将其附加到
如果AValue[I],“那么
S:=S+AValue[I]
其他的
//如果char是逗号,则将S添加到临时列表并重置S
开始
L.添加(S);
S:='';
终止
终止
//将最后一个字符串添加到临时列表
L.添加(S);
//将临时列表中的项目分配给要传回的变量
Col0:=L[0];
Col1:=L[1];
Col2:=L[2];
Col3:=L[3];
Col4:=L[4];
Col5:=L[5];
最后
L.免费;
终止
终止

我提出了自己的解决方案,使用了TStringList和名称/值对,看起来效果不错。我将项目标题存储在名称中,并将所有子列存储在值中,然后只解析各个子项目的值

procedure LoadFromFile(AListView: TListView; AFileName: string);
var
 I : Integer;
 SL: TStringList;
 Item: TListItem;
 Col0, Col1, Col2, Col3, Col4, Col5: String;
begin
  SL:= TStringList.Create;
  try
    SL.LoadFromFile(AFileName);
    AListView.Items.BeginUpdate;
    AListView.Items.Clear;
    for I:=  0 to SL.Count - 1 do
    begin
     Item:= AlistView.Items.Add;
     Item.Caption:= SL.Names[I];
     parseValue(SL.ValueFromIndex[I], Col0, Col1, Col2, Col3, Col4, Col5);
     Item.SubItems.Add(Col0);
     Item.SubItems.Add(Col1);
     Item.SubItems.Add(Col2);
     Item.SubItems.Add(Col3);
     Item.SubItems.Add(Col4);
     Item.SubItems.Add(Col5);
    end;
    AListView.Items.EndUpdate;
  finally
   SL.Free;
  end;
end;

    procedure SaveToFile(AListView: TListView; AFileName: string);
    var
     I: Integer;
     SL: TStringList;
    begin
     SL:= TStringList.Create;
     for I := 0 to AListView.Items.Count - 1 do
     begin
      SL.Add(AlistView.Items[I].Caption + '=' +
             AlistView.Items[I].SubItems[0] + ',' +
             AlistView.Items[I].SubItems[1] + ',' +
             AlistView.Items[I].SubItems[2] + ',' +
             AlistView.Items[I].SubItems[3] + ',' +
             AlistView.Items[I].SubItems[4] + ',' +
             AlistView.Items[I].SubItems[5])
     end;
     try
      SL.SaveToFile(AFileName);
     finally
       SL.Free;
     end;
    end;

 procedure parseValue(AValue: String; var Col0, Col1, Col2, Col3, Col4, Col5: String);
 var
  I: Integer;
  S: String;
  L: TStringList;
 begin
  //create a temporary list to store the data parts in
  L:= TStringList.Create;
  try
  //iterate through the length of the string
  for I:= 1 to length(AValue) do
  begin
   //if the char is not a comma, append it to S
   if AValue[I] <> ',' then
    S:= S + AValue[I]
   else
   // if char is a comma, add S to temporary list and reset S
   begin
    L.Add(S);
    S:= '';
   end;
  end;
  //add the last string to temporary list
  L.Add(S);
  //assign the items in temporary list to variables to be passed back 
  Col0:= L[0];
  Col1:= L[1];
  Col2:= L[2];
  Col3:= L[3];
  Col4:= L[4];
  Col5:= L[5];
  finally
   L.Free;
  end;
 end;
过程LoadFromFile(AListView:TListView;AFileName:string);
变量
I:整数;
SL:TStringList;
项目:技术指标;
Col0,Col1,Col2,Col3,Col4,Col5:字符串;
开始
SL:=TStringList.Create;
尝试
SL.LoadFromFile(文件名);
AListView.Items.BeginUpdate;
AListView.Items.Clear;
对于I:=0到SL。计数-1 do
开始
项目:=AlistView.Items.Add;
项目名称:=SL.Names[I];
parseValue(SL.ValueFromIndex[I],Col0,Col1,Col2,Col3,Col4,Col5);
项目.子项目.添加(Col0);
项目.子项目.添加(第1栏);
项目.子项目.添加(Col2);
项目.子项目.添加(第3栏);
项目.子项目.添加(第4栏);
项目.子项目.添加(第5栏);
终止
AListView.Items.EndUpdate;
最后
SL.免费;
终止
终止
过程SaveToFile(AListView:TListView;AFileName:string);
变量
I:整数;
SL:TStringList;
开始
SL:=TStringList.Create;
对于I:=0到AListView.Items.Count-1 do
开始
SL.Add(AlistView.Items[I].标题+'='+
AlistView.Items[I]。子项[0]+,'+
AlistView.Items[I]。子项[1]+,'+
AlistView.Items[I].子项[2]+','+
AlistView.Items[I].子项[3]+','+
AlistView.Items[I].子项[4]+','+
AlistView.Items[I].子项[5])
终止
尝试
SL.SaveToFile(文件名);
最后
SL.免费;
终止
终止
过程解析值(AValue:String;var Col0、Col1、Col2、Col3、Col4、Col5:String);
变量
I:整数;
S:字符串;
L:TStringList;
开始
//创建临时列表以存储数据部分
L:=TStringList.Create;
尝试
//遍历字符串的长度
对于I:=1到长度(AValue)do
开始
//如果字符不是逗号,请将其附加到
如果AValue[I],“那么
S:=S+AValue[I]
其他的
//如果char是逗号,则将S添加到临时列表并重置S
开始
L.添加(S);
S:='';
终止
终止
//将最后一个字符串添加到临时列表
L.添加(S);
//将临时列表中的项目分配给要传回的变量
Col0:=L[0];
Col1:=L[1];
Col2:=L[2];
Col3:=L[3];
Col4:=L[4];
Col5:=L[5];
最后
L.免费;
终止
终止

现在,您已经到了意识到这一点的时候了。@TLama是对的,您真的不想制作视觉效果