Einzelnen Beitrag anzeigen

Popov
(Gast)

n/a Beiträge
 
#12

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 01:17
Delphi-Quellcode:
const
  IniSelRC = 'Sel3';
var
  x, y, k: Integer;
  IniFile, s, xs, ys: String;
  sl: TStringList;
  Ini: TIniFile;
begin
  IniFile := ChangeFileExt(ParamStr(0), '.ini');
  with StringGrid1 do
  begin
    Ini := TIniFile.Create(IniFile);
    try
      //Löscht den alten Inhalt
      Ini.EraseSection(IniSelRC);

      k := 0;
      for x := 0 to ColCount - 1 do
        for y := 0 to RowCount - 1 do
        begin
          Inc(k);
          Ini.WriteString(IniSelRC, IntToStr(k), Format('%d:%d:%s', [x, y, Cells[x, y]]));
        end;
    finally
      Ini.Free;
    end;

    for x := 0 to ColCount do
      for y := 0 to RowCount do
        Cells[x, y] := '';

    ColCount := 0;
    RowCount := 0;

    Ini := TIniFile.Create(IniFile);
    try
      sl := TStringList.Create;
      try
        Ini.ReadSection(IniSelRC, sl);

        for k := 0 to sl.Count - 1 do
        begin
          s := Ini.ReadString(IniSelRC, sl[k], '');
          xs := System.Copy(s, 1, Pos(':', s) - 1);
          System.Delete(s, 1, Pos(':', s));
          ys := System.Copy(s, 1, Pos(':', s) - 1);
          System.Delete(s, 1, Pos(':', s));

          //Fügt Splaten und Zeilen bei Bedarf
          if ColCount < (StrToInt(xs) + 1) then
            ColCount := StrToInt(xs) + 1;
          if RowCount < (StrToInt(ys) + 1) then
            RowCount := StrToInt(ys) + 1;

          //Fügt Fixed-Stalten und -Zeilen bei Bedarf
          if (ColCount> 1) and (FixedCols = 0) then
            FixedCols := 1;
          if (RowCount > 1) and (FixedRows = 0) then
            FixedRows := 1;

          Cells[StrToInt(xs), StrToInt(ys)] := s;
        end;
      finally
        sl.Free;
      end; {}
    finally
      Ini.Free;
    end;
  end;
end;
Nicht groß geprüft, sollte aber funktionieren.
  Mit Zitat antworten Zitat