Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Problem mit Speichern von Combobox-Items in Inifile (https://www.delphipraxis.net/18328-problem-mit-speichern-von-combobox-items-inifile.html)

Dani 16. Mär 2004 19:03


Problem mit Speichern von Combobox-Items in Inifile
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo Allerseits,

Ich versuche gerade verzweifelt, den Inhalt einer Combobox (mit Dropdown) beim Schließen meines Programms in einer Ini-Datei zu speichern... das Problem ist: Nach jeden Neustart, also beim laden der Ini-Einstellungen, fehlt plötzlich der letzte Eintrag, sodass die Liste mit jedem Neustart des Programms ein bischen kürzer wird. Ich bin aber nicht in der Lage, den Fehler im Code zu finden :gruebel:
Laut Debugger wird die Schleife in SetBoxItems genau einmal zu wenig durchlaufen. Es werden alle Einträge in die Ini-Datei geschrieben, es liegt also vermutlich nicht am Speichervorgang, sondern eher am Ladevorgang... ich finde trotzdem keinen Fehler :oops:

Hat jemand bessere Augen als ich?
Hier der betreffende Code:

Delphi-Quellcode:

//Konstanten
const
 NUMDIV = 'NUM_DIV';
 NUMMAL = 'NUM_MAL';
 NUMMINUS = 'NUM_MINUS';
 INI_SEP = #20;


//Items aus Ini-Key in Combobox schreiben
procedure SetBoxItems(Items: TStrings; InputStr: String);
var i: Integer;
begin
 If trim(InputStr)='' then exit;
 While pos(INI_SEP, InputStr)<> 0 do
  begin
   Items.Add( StringReplace(copy(InputStr,1,pos(INI_SEP, InputStr)-1), #13#10, '', [rfReplaceAll]) );
   Delete(InputStr, 1, pos(INI_SEP, InputStr));
  end;
end;


//combobox Items und anderes aus ini Datei laden
procedure TForm1.FormCreate(Sender: TObject);
var ini: Tinifile;
    i: Integer;
begin
Randomize;
Inifilepath := ExtractFilePath(Application.ExeName) + 'skSettings.ini';

Syshotkey1.Add(HotKeyItem(vkDivide,[] ));
Syshotkey1.Add(HotKeyItem(vkMultiply,[] ));
Syshotkey1.Add(HotKeyItem(vkSubtract,[] ));
Syshotkey1.Add(HotKeyItem(vkHome,[] ));

ini := TInifile.Create(Inifilepath);
 try
  SetBoxItems(Box1.Items, ini.ReadString(NUMDIV, 'Items', ''));
  Box1.ItemIndex := ini.ReadInteger(NUMDIV, 'LastIndex', -1);
  Rand1.Checked := ini.ReadBool(NUMDIV, 'Randomize', false);

  SetBoxItems(Box2.Items, ini.ReadString(NUMMAL, 'Items', ''));
  Box2.ItemIndex := ini.ReadInteger(NUMMAL, 'LastIndex', -1);
  Rand2.Checked := ini.ReadBool(NUMMAL, 'Randomize', false);

  SetBoxItems(Box3.Items, ini.ReadString(NUMMINUS, 'Items', ''));
  Box3.ItemIndex := ini.ReadInteger(NUMMINUS, 'LastIndex', -1);
  Rand3.Checked := ini.ReadBool(NUMMINUS, 'Randomize', false);
 finally
  ini.Free;
 end;

//Combobox Items in formatierten String umwandeln. Separator = #20
function GatherItems(aBox: TCombobox): String;
var i: Integer;
begin
 Result := '';
 for i:=0 to aBox.Items.Count-1 do
  Result := Result + aBox.Items[i] + INI_SEP;
end;


//Combobox und anderes in Ini speichern
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var ini: TInifile;
    i: Integer;
begin
 CanClose := true;

 DeleteFile(Inifilepath);
 ini := Tinifile.Create(Inifilepath);
 try
  Ini.WriteString( NUMDIV, 'Items', GatherItems(Form1.Box1) );
  Ini.WriteInteger( NUMDIV, 'LastIndex', Form1.Box1.Items.IndexOf(Form1.Box1.Text) );
  Ini.WriteBool(NUMDIV, 'Randomize', Form1.Rand1.Checked);

  Ini.WriteString( NUMMAL, 'Items', GatherItems(Form1.Box2) );
  Ini.WriteInteger( NUMMAL, 'LastIndex', Form1.Box2.Items.IndexOf(Form1.Box2.Text) );
  Ini.WriteBool(NUMMAL, 'Randomize', Form1.Rand2.Checked);

  Ini.WriteString( NUMMINUS, 'Items', GatherItems(Form1.Box3) );
  Ini.WriteInteger( NUMMINUS, 'LastIndex', Form1.Box3.Items.IndexOf(Form1.Box3.Text) );
  Ini.WriteBool(NUMMINUS, 'Randomize', Form1.Rand3.Checked);


 finally
  ini.UpdateFile;
  ini.Free;
 end;
end;
 
end;
:drunken: Gruß,
Dani

NicoDE 16. Mär 2004 19:18

Re: Problem mit Speichern von Combobox-Items in Inifile
 
Anstatt Dir die Arbeit zu machen, könntest Du CommaText (Eigenschaft von TComboBox.Items) verwenden...
Delphi-Quellcode:
var
  ComboBoxItems: string;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ComboBoxItems := ComboBox1.Items.CommaText;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ComboBox1.Items.CommaText := ComboBoxItems;
end;

Dani 16. Mär 2004 19:20

Re: Problem mit Speichern von Combobox-Items in Inifile
 
Hmm sehr nützlich, danke!
Ich werds mal so versuchen...

Edit: Klappt auch nicht so recht, das erste Item wird nicht richtig gespeichert, sondern jedes Wort als eigene Zeile... dann halt SaveToFile und LoadFromFile...

Dani 16. Mär 2004 19:55

Re: Problem mit Speichern von Combobox-Items in Inifile
 
Edit: Problem hatte damit zu tun, dass ItemIndex auf -1 gesetzt wurde. Das scheint mal eben alle Items der Combobox unter mysteriösen Umständen zu löschen :freak:


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:58 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