我正在尝试将一个简单的时间表(urnik.txt)加载到我的字符串网格中。首先我检查文件是否存在,如果不存在,则创建它,否则加载它。
procedure TForm1.FormCreate(Sender: TObject);
var
i, j, k: Integer;
Seznam: TStrings;
ApplicationPath: string;
const
dan = ' PoToSrČePe';
begin
ApplicationPath := ExtractFileDir(Application.ExeName);
if not FileExists(ApplicationPath + '\Urnik.txt') then
begin
Seznam := TStringList.Create;
try
for i := 0 to AdvStringGrid1.ColCount - 1 do
AdvStringGrid1.Cells[i, 0] := Copy(dan, 2 * i + 1, 2);
for i := 1 to AdvStringGrid1.RowCount - 1 do
AdvStringGrid1.Cells[0, i] := IntToStr(i) + '.ura';
for i := 0 to AdvStringGrid1.ColCount - 1 do
Seznam.AddStrings(advStringGrid1.Cols[i]);
for i := 0 to AdvStringGrid1.RowCount - 1 do
Seznam.AddStrings(advStringGrid1.rows[j]);
Seznam.SaveToFile(ApplicationPath + '\Urnik.txt');
finally
Seznam.free;
end;
end
else
Seznam := TStringList.Create;
try
Seznam.LoadFromFile(ApplicationPath + '\Urnik.txt');
k := 0;
for i := 0 to AdvStringGrid1.ColCount - 1 do
for j := 0 to AdvStringGrid1.RowCount - 1 do
begin
AdvStringGrid1.Cells[i, j] := Seznam.Strings[k];
Inc(k);
end;
finally
Seznam.free;
end;
end;
为了保存我使用的字符串网格的内容:
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
i, j: Integer;
Seznam: TStrings;
ApplicationPath: string;
begin
ApplicationPath := ExtractFileDir(Application.ExeName);
Seznam := TStringList.Create;
try
for i := 0 to AdvStringGrid1.ColCount - 1 do
Seznam.AddStrings(AdvStringGrid1.Cols[i]);
Seznam.SaveToFile(ApplicationPath + '\Urnik.txt');
finally
Seznam.free;
end;
end;
但是,如果该文件不存在,我会收到错误消息:
我究竟做错了什么 ??
procedure TForm1.FormCreate(Sender: TObject);
Var
I,j,k:Integer;
ApplicationPath: string;
Seznam:TStrings;
Const dan = ' PoToSrČePe';
begin
ApplicationPath := ExtractFileDir(Application.ExeName);
if not FileExists(ApplicationPath + '\Urnik.txt') then
begin
Seznam := TStringList.Create;
try
for i := 0 to AdvStringGrid1.ColCount - 1 do
AdvStringGrid1.Cells[i, 0] := Copy(dan, 2 * i + 1, 2);
for i := 1 to AdvStringGrid1.RowCount - 1 do
AdvStringGrid1.Cells[0, i] := IntToStr(i) + '.ura';
for i := 0 to AdvStringGrid1.ColCount - 1 do
Seznam.AddStrings(advStringGrid1.Cols[i]);
for i := 0 to AdvStringGrid1.RowCount - 1 do
Seznam.AddStrings(advStringGrid1.rows[j]);
Seznam.SaveToFile(ApplicationPath + '\Urnik.txt');
finally
Seznam.free;
end;
end
else
begin
Seznam := TStringList.Create;
try
Seznam.LoadFromFile(ApplicationPath + '\Urnik.txt');
k := 0;
for i := 0 to AdvStringGrid1.ColCount - 1 do
for j := 0 to AdvStringGrid1.RowCount - 1 do
begin
AdvStringGrid1.Cells[i, j] := Seznam.Strings[k];
Inc(k);
end;
finally
Seznam.free;
end;
end;
end;
这种作品......我希望它没问题......