【发布时间】:2010-09-05 09:24:01
【问题描述】:
有人可以知道/给我一个示例,说明如何将 ini 文件中的部分读入 stringGrid 吗?因为我正在努力弄清楚该怎么做。
谢谢
科林
【问题讨论】:
标签: delphi
有人可以知道/给我一个示例,说明如何将 ini 文件中的部分读入 stringGrid 吗?因为我正在努力弄清楚该怎么做。
谢谢
科林
【问题讨论】:
标签: delphi
托姆:
procedure ReadIntoGrid(const aIniFileName, aSection: string; const aGrid: TStringGrid);
var
Ini: TIniFile;
SL: TStringList;
i: Integer;
begin
SL := TStringList.Create;
try
Ini := TIniFile.Create(aIniFileName);
try
aGrid.ColCount := 2;
Ini.ReadSectionValues(aSection, SL);
aGrid.RowCount := SL.Count;
for i := 0 to SL.Count - 1 do
begin
aGrid.Cells[0,i] := SL.Names[i];
aGrid.Cells[1,i] := SL.ValueFromIndex[i];
end;
finally
Ini.Free;
end;
finally
SL.Free;
end;
end;
编辑
反过来:
procedure SaveFromGrid(const aIniFileName, aSection: string; const aGrid: TStringGrid);
var
Ini: TIniFile;
i: Integer;
begin
Ini := TIniFile.Create(aIniFileName);
try
for i := 0 to aGrid.RowCount - 1 do
Ini.WriteString(aSection, aGrid.Cells[0,i], aGrid.Cells[1,i]);
finally
Ini.Free;
end;
end;
【讨论】:
你最好使用TValueListEditor来显示一个ini文件的一部分。
这是一个简单的演示代码:
procedure TForm1.Button1Click(Sender: TObject);
var
SL: TStrings;
IniFile: TMemIniFile;
begin
SL:= TStringList.Create;
try
IniFile:= TMemIniFile.Create('test.ini');
try
IniFile.ReadSectionValues('FOLDERS', SL);
ValueListEditor1.Strings.Assign(SL);
finally
IniFile.Free;
end;
finally
SL.Free;
end;
end;
【讨论】: