【问题标题】:ini file section to stringgridini 文件部分到 stringgrid
【发布时间】:2010-09-05 09:24:01
【问题描述】:

有人可以知道/给我一个示例,说明如何将 ini 文件中的部分读入 stringGrid 吗?因为我正在努力弄清楚该怎么做。

谢谢

科林

【问题讨论】:

    标签: delphi


    【解决方案1】:

    托姆:

    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;
    

    【讨论】:

      【解决方案2】:

      你最好使用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;
      

      【讨论】:

        猜你喜欢
        • 2016-04-19
        • 1970-01-01
        • 2018-08-08
        • 2014-03-22
        • 2011-10-24
        • 2015-08-08
        • 2013-09-13
        • 2011-05-30
        • 2020-12-27
        相关资源
        最近更新 更多