读书人

100分![listview!]★ listview实现 保

发布时间: 2012-03-04 11:13:33 作者: rapoo

100分!★★[listview!!]★★ listview实现 保存 与 读取 !
假设有一个listview如下
name | age
------|-----
tom | 20
mary | 18

.......

怎么把它保存成文本? 以及如何把这个文本重新导入到listview中呢?

我在网上找了很久,自己也研究了很久,我有下面一段代码,是关于保存的,可是为什么保存不出来呢(保存出来的文本仅什么都没有,我想可能是j的赋值有问题)?

var
F:textfile;
i,j:shortint;
str:string;
begin
AssignFile(F,ExtractFileDir(application.ExeName)+ '\ProcessDATA '+FormatDateTime( ' "- "YYYY "- "MM "- "DD "- " ',now)+FormatDateTime( 'hh "- "mm "- "ss ',Time)+ '.txt ');
ReWrite(F);
j:= istview1.Items.Count;


for i := 0 to j-1 do
begin
str := str + 'name: ' + ListView1.Items[i].Caption + chr(13) + chr(10);
str := str + 'age: ' + ListView1.Items[i].SubItems[0] + chr(13) + chr(10);
str := str + chr(13) + chr(10);
end;
Writeln(F,str);
CloseFile(F);

end;

至于 读取列表的问题,烦请大家帮忙想想办法!谢谢!

[解决办法]
保存列表内容:
var
F: textfile;
i, j: shortint;
str: string;
begin
AssignFile(F, ExtractFileDir(application.ExeName) + '\ProcessDATA ' + FormatDateTime( ' "- "YYYY "- "MM "- "DD "- " ', now) + '.txt ');
ReWrite(F);
j := ListView1.Items.Count;
for i := 0 to j - 1 do
begin
str := str + 'name: ' + ListView1.Items[i].Caption + chr(13) + chr(10);
str := str + 'age: ' + ListView1.Items[i].SubItems[0] + chr(13) + chr(10);
str := str + chr(13) + chr(10);
end;
Writeln(F, str);
CloseFile(F);
end;

读取列表:
var
f: TextFile;
v_filename: string;
v_Str: string;
vLstItem: TListItem;
vcount: Integer;
begin
ListView1.Items.Clear;
AssignFile(F, ExtractFileDir(application.ExeName) + '\ProcessDATA ' + FormatDateTime( ' "- "YYYY "- "MM "- "DD "- " ', now) + '.txt ');
try
Reset(f);
vcount := 0;
while not Eof(f) do
begin
Inc(vcount);
Readln(f, v_str);
case vcount of
1:
begin
vLstItem := ListView1.Items.Add;
vLstItem.Caption := Copy(v_Str, pos( 'name: ', v_Str) + length( 'name: '), Length(v_Str));
end;
2:
vLstItem.SubItems.Add(Copy(v_Str, Pos( 'age: ', v_Str) + length( 'age: '), Length(v_Str)));
3:
vcount := 0;
end;
end;
finally
CloseFile(f);
end;
end;
[解决办法]
//***********read
var
f: TextFile;
v_filename: string;
v_Str: string;
vLstItem: TListItem;
vcount: Integer;
begin
ListView1.Items.Clear;
AssignFile(F, ExtractFileDir(application.ExeName) + '\ProcessDATA ' + FormatDateTime( ' "- "YYYY "- "MM "- "DD "- " ', now) + '.txt ');


try
Reset(f);
vcount := 0;
while not Eof(f) do
begin
Inc(vcount);
Readln(f, v_str);
case vcount of
1:
begin
vLstItem := ListView1.Items.Add;
vLstItem.Caption := Copy(v_Str, pos( 'name: ', v_Str) + length( 'name: '), Length(v_Str));
end;
2:
vLstItem.SubItems.Add(Copy(v_Str, Pos( 'age: ', v_Str) + length( 'age: '), Length(v_Str)));
3:
vcount := 0;
end;
end;
finally
CloseFile(f);
end;
end;

读书人网 >.NET

热点推荐