AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Stringgrid in INI file schreiben

Ein Thema von rhodan · begonnen am 25. Apr 2014 · letzter Beitrag vom 26. Apr 2014
Antwort Antwort
Seite 2 von 3     12 3      
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#11

AW: Stringgrid in INI file schreiben

  Alt 25. Apr 2014, 23:58
Warum speicherst du in der Ini nicht (mehr) die Informationen von Zeilen- und Spaltenanzahl?
Das würde doch ungemein helfen, oder?

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

Ach ja, 4 Spalten von 0 an gezählt gehen von 0 bis 3
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo (26. Apr 2014 um 00:00 Uhr)
  Mit Zitat antworten Zitat
Popov
(Gast)

n/a Beiträge
 
#12

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 00: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
Benutzerbild von rhodan
rhodan

Registriert seit: 4. Okt 2005
Ort: Hamburg
150 Beiträge
 
Delphi 7 Personal
 
#13

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 00:19
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
  Mit Zitat antworten Zitat
Popov
(Gast)

n/a Beiträge
 
#14

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 00:24
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.
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#15

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 00:33
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=...
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Popov
(Gast)

n/a Beiträge
 
#16

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 00:40
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.
  Mit Zitat antworten Zitat
Popov
(Gast)

n/a Beiträge
 
#17

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 00:51
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;
  Mit Zitat antworten Zitat
Benutzerbild von rhodan
rhodan

Registriert seit: 4. Okt 2005
Ort: Hamburg
150 Beiträge
 
Delphi 7 Personal
 
#18

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 01:19
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;
[DCC Fehler] source.pas(54): E2003 Undeklarierter Bezeichner: 'RowCount' kein plan warum er den nicht erkennt :S
  Mit Zitat antworten Zitat
Popov
(Gast)

n/a Beiträge
 
#19

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 01:31
RowCount ist eine Eigenschaft von StringGrid. Du hast das 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.
  Mit Zitat antworten Zitat
Benutzerbild von rhodan
rhodan

Registriert seit: 4. Okt 2005
Ort: Hamburg
150 Beiträge
 
Delphi 7 Personal
 
#20

AW: Stringgrid in INI file schreiben

  Alt 26. Apr 2014, 01:41
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

Geändert von rhodan (26. Apr 2014 um 01:55 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 3     12 3      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:16 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz