Delphi-PRAXiS
Seite 2 von 3     12 3      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Stringgrid in INI file schreiben (https://www.delphipraxis.net/180136-stringgrid-ini-file-schreiben.html)

Sir Rufo 25. Apr 2014 23:58

AW: Stringgrid in INI file schreiben
 
Warum speicherst du in der Ini nicht (mehr) die Informationen von Zeilen- und Spaltenanzahl?
Das würde doch ungemein helfen, oder?

Und das
Delphi-Quellcode:
ini.EraseSection
kannst du direkt vor die
Delphi-Quellcode:
for
-Schleife setzen.

Ach ja, 4 Spalten von 0 an gezählt gehen von 0 bis 3 ;)

Popov 26. Apr 2014 00:17

AW: Stringgrid in INI file schreiben
 
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.

rhodan 26. Apr 2014 00:19

AW: Stringgrid in INI file schreiben
 
ok..mit dieser änderung hab ichs hinbekommen das keine unnötigen leeren zeilen abgespeichert werden:
Delphi-Quellcode:
 begin
    ini := TIniFile.Create(ExtractFilePath(ParamStr(0))+'myinifile.ini');
    try
        for x := 2 to ColCount do
        Ini.WriteString('Sel', IntToStr(x-1), Cols[x-1].CommaText);
    finally
      Ini.Free;
    end;
hilf mir bitte wie ich beim einlesen die nötigen columns erzeuge..ich kriegs nicht hin :F

Popov 26. Apr 2014 00:24

AW: Stringgrid in INI file schreiben
 
Achso, du hast das erste Beispiel genommen.

Ich würde da nicht groß fackeln und eine Extra-Sektion erstellen in der du die Anzahl Cols und Rows speicherst.

Sir Rufo 26. Apr 2014 00:33

AW: Stringgrid in INI file schreiben
 
Zitat:

Zitat von Popov (Beitrag 1257012)
Achso, du hast das erste Beispiel genommen.

Ich würde da nicht groß fackeln und eine Extra-Sektion erstellen in der du die Anzahl Cols und Rows speicherst.

Wofür sollte denn eine extra Sektion benötigt werden?
Code:
[Sel]
ColCount=4
RowCount=4
FixedColCount=1
FixedRowCount=1
0=...
1=...
2=...
3=...

Popov 26. Apr 2014 00:40

AW: Stringgrid in INI file schreiben
 
Zitat:

Zitat von Sir Rufo (Beitrag 1257013)
Wofür sollte denn eine extra Sektion benötigt werden?

Jep. Ich war gedanklich noch im zweiten Beispiel und da steht jeder Ident für eine Zelle.

Popov 26. Apr 2014 00:51

AW: Stringgrid in INI file schreiben
 
Der erste Code könnte so aussehen:
Delphi-Quellcode:
const
  IniSelRC = 'Sel1';
  IniRowCount = 'RowCount';
  IniColCount = 'ColCount';
  IniFixedRows = 'FixRows';
  IniFixedCols = 'FixCols';
var
  x, y: Integer;
  IniFile: String;
  Ini: TIniFile;
begin
  IniFile := ChangeFileExt(ParamStr(0), '.ini');
  with StringGrid1 do
  begin
    //Speichern
    Ini := TIniFile.Create(IniFile);
    try
      //Löscht den alten Inhalt
      Ini.EraseSection(IniSelRC);

      for x := 0 to ColCount do
        Ini.WriteString(IniSelRC, IntToStr(x), Cols[x].CommaText);

      Ini.WriteInteger(IniSelRC, IniRowCount, RowCount);
      Ini.WriteInteger(IniSelRC, IniColCount, ColCount);
      Ini.WriteInteger(IniSelRC, IniFixedRows, FixedRows);
      Ini.WriteInteger(IniSelRC, IniFixedCols, FixedCols);
    finally
      Ini.Free;
    end;

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

    ColCount := 0;
    RowCount := 0;

    //Lesen
    Ini := TIniFile.Create(IniFile);
    try
      RowCount := Ini.ReadInteger(IniSelRC, IniRowCount, RowCount);
      ColCount := Ini.ReadInteger(IniSelRC, IniColCount, ColCount);
      FixedRows := Ini.ReadInteger(IniSelRC, IniFixedRows, FixedRows);
      FixedCols := Ini.ReadInteger(IniSelRC, IniFixedCols, FixedCols);

      for x := 0 to ColCount do
        Cols[x].CommaText := Ini.ReadString(IniSelRC, IntToStr(x), '');
    finally
      Ini.Free;
    end;
  end;
end;

rhodan 26. Apr 2014 01:19

AW: Stringgrid in INI file schreiben
 
hab jetzt auf verschiedene weisen rumprobiert aber ich kriege immer ne fehlermeldung...beim lesen aus der ini:
Delphi-Quellcode:
procedure TForm2.btnreadClick(Sender: TObject);
const
  IniSelRC = 'Sel1';
  IniRowCount = 'RowCount';
  IniColCount = 'ColCount';
  IniFixedRows = 'FixRows';
  IniFixedCols = 'FixCols';
var
  x, y: Integer;
  IniFile: String;
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(IniFile);
    try
      RowCount := Ini.ReadInteger(IniSelRC, IniRowCount, RowCount);
      ColCount := Ini.ReadInteger(IniSelRC, IniColCount, ColCount);
      FixedRows := Ini.ReadInteger(IniSelRC, IniFixedRows, FixedRows);
      FixedCols := Ini.ReadInteger(IniSelRC, IniFixedCols, FixedCols);

      for x := 0 to ColCount do
        Cols[x].CommaText := Ini.ReadString(IniSelRC, IntToStr(x), '');
    finally
      Ini.Free;
    end;
  end;
  end;
Delphi-Quellcode:
[DCC Fehler] source.pas(54): E2003 Undeklarierter Bezeichner: 'RowCount'
kein plan warum er den nicht erkennt :S

Popov 26. Apr 2014 01:31

AW: Stringgrid in INI file schreiben
 
RowCount ist eine Eigenschaft von StringGrid. Du hast das
Delphi-Quellcode:
With StringGrid1 do
vergessen. In meinem Code steht:
Delphi-Quellcode:
begin
...
  with StringGrid1 do
  begin
...
    try
...
      Ini.WriteInteger(IniSelRC, IniRowCount, RowCount);
Mit with kann man sich sparen StringGrid1 immer wieder zu schreiben. Ohne with würde das so aussehen:
Delphi-Quellcode:
begin
...
  //with StringGrid1 do
  begin
...
    try
...
      Ini.WriteInteger(IniSelRC, IniRowCount, StringGrid1.RowCount);
Das gleiche natürlich auch bei ColCount, Cells usw.

rhodan 26. Apr 2014 01:41

AW: Stringgrid in INI file schreiben
 
ok habs hinbekommen...schreibmethode:

Delphi-Quellcode:
//schreiben
procedure TForm2.btnwriteClick(Sender: TObject);
const
  IniSelRC = 'Sel1';
  IniRowCount = 'RowCount';
  IniColCount = 'ColCount';
  IniFixedRows = 'FixRows';
  IniFixedCols = 'FixCols';
var
  x, y: Integer;
  IniFile: String;
  Ini: TIniFile;
begin
  IniFile := ChangeFileExt(ParamStr(0), '.ini');
  with strgrid do
  begin
    //Speichern
    Ini :=TIniFile.Create(ExtractFilePath(ParamStr(0))+'myinifile.ini');
    try
      //Löscht den alten Inhalt
      Ini.EraseSection(IniSelRC);

      for x := 0 to ColCount do
        Ini.WriteString(IniSelRC, IntToStr(x), Cols[x].CommaText);

      Ini.WriteInteger(IniSelRC, IniRowCount, RowCount);
      Ini.WriteInteger(IniSelRC, IniColCount, ColCount);
      Ini.WriteInteger(IniSelRC, IniFixedRows, FixedRows);
      Ini.WriteInteger(IniSelRC, IniFixedCols, FixedCols);
    finally
      Ini.Free;
    end;
  end;
end;
ini sieht dann so aus:
Delphi-Quellcode:
[Sel1]
0=,,
1=,rhodan,10
2=,robin,20
3=,,
RowCount=3
ColCount=3
FixRows=1
FixCols=1
lesemethode:
Delphi-Quellcode:
procedure TForm2.btnreadClick(Sender: TObject);
const
  IniSelRC = 'Sel1';
  IniRowCount = 'RowCount';
  IniColCount = 'ColCount';
  IniFixedRows = 'FixRows';
  IniFixedCols = 'FixCols';
var
  x, y: Integer;
  IniFile: String;
  Ini: TIniFile;
begin
  IniFile := ChangeFileExt(ParamStr(0), '.ini');
  with strgrid do
  begin
  Ini := TIniFile.Create(IniFile);
    try
      RowCount := Ini.ReadInteger(IniSelRC, IniRowCount, RowCount);
      ColCount := Ini.ReadInteger(IniSelRC, IniColCount, ColCount);
      FixedRows := Ini.ReadInteger(IniSelRC, IniFixedRows, FixedRows);
      FixedCols := Ini.ReadInteger(IniSelRC, IniFixedCols, FixedCols);

      for x := 0 to ColCount do
        Cols[x].CommaText := Ini.ReadString(IniSelRC, IntToStr(x), '');
    finally
      Ini.Free;
    end;
  end;
  end;
passiert leider gar nichts.... hab grad nen blackout....:S

schreiben funktioniert..aber lesen nicht


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:37 Uhr.
Seite 2 von 3     12 3      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz