【问题标题】:Read all section .ini values into stringgrid将所有部分 .ini 值读入 stringgrid
【发布时间】:2016-04-19 14:22:52
【问题描述】:

我使用 Delphi XE7。有没有办法将 .ini 文件中的所有值加载到不同列的字符串网格中?

我的 .ini 文件看起来像

[1038] 值 = a1 B值 = b1 C值 = c1 D值 = d1 [1031] A值 = a2 B值 = b2 C值 = c2 D值 = d2

我使用这个程序来填充网格:

procedure TForm1.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;

效果很好,我明白了:

我的问题是……
如何将所有部分值(1038 和 1031)读入 1038 值旁边的网格中?值将始终固定。

【问题讨论】:

  • 这个任务的哪一部分你觉得困难。从表面上看,您似乎知道如何读取 INI 文件,以及如何使用字符串网格。看来您拥有所需的一切。
  • 我必须同意大卫的观点。唯一棘手的部分似乎是将不同部分的项目名称同步到字符串网格中。

标签: delphi delphi-xe delphi-xe7


【解决方案1】:

给你一些想法:

首先,我认为您应该在程序中添加一个参数:

procedure TForm1.ReadIntoGrid(const aIniFileName, aSection: string;
const aGrid: TStringGrid; const aColumn: Integer = 1);

其次,重写这部分方法:

  for i := 0 to SL.Count - 1 do
  begin
    aGrid.Cells[0,i] := SL.Names[i];
    aGrid.Cells[1,i] := SL.ValueFromIndex[i];
  end;

替换为

  for i := 0 to SL.Count - 1 do
  begin
    aGrid.Cells[0,i] := SL.Names[i];
    aGrid.Cells[aColumn,i] := SL.ValueFromIndex[i];
  end;

ps:显然你不需要将值重写到第一列。

所以现在假设你正在调用这样的方法:

ReadIntoGrid('MyIniFile.ini','1038', MyGrid, 1);
ReadIntoGrid('MyIniFile.ini','1031', MyGrid, 2);

【讨论】:

  • 如果部分内的项目不同或顺序不同,则可能会失败。
  • 是的,假设我们比较相同的项目
  • 建议:aGrid.Rows(0).BeginUpdate; try ReadIntoGrid(...); ReadIntoGrid(...); finally aGrid.Rows(0).EndUpdate end; 减少闪烁并增加一些速度
猜你喜欢
  • 1970-01-01
  • 2020-06-27
  • 2011-07-24
  • 2011-10-28
  • 2012-03-29
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
相关资源
最近更新 更多