AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Sonstige Fragen zu Delphi Delphi Problem mit Speichern von Combobox-Items in Inifile
Thema durchsuchen
Ansicht
Themen-Optionen

Problem mit Speichern von Combobox-Items in Inifile

Ein Thema von Dani · begonnen am 16. Mär 2004 · letzter Beitrag vom 16. Mär 2004
Antwort Antwort
Benutzerbild von Dani
Dani

Registriert seit: 19. Jan 2003
732 Beiträge
 
Turbo Delphi für Win32
 
#1

Problem mit Speichern von Combobox-Items in Inifile

  Alt 16. Mär 2004, 19:03
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
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

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;
Gruß,
Dani
Angehängte Dateien
Dateityp: rar unit1.rar (2,9 KB, 4x aufgerufen)
Dani H.
  Mit Zitat antworten Zitat
NicoDE
(Gast)

n/a Beiträge
 
#2

Re: Problem mit Speichern von Combobox-Items in Inifile

  Alt 16. Mär 2004, 19:18
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;
  Mit Zitat antworten Zitat
Benutzerbild von Dani
Dani

Registriert seit: 19. Jan 2003
732 Beiträge
 
Turbo Delphi für Win32
 
#3

Re: Problem mit Speichern von Combobox-Items in Inifile

  Alt 16. Mär 2004, 19:20
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 H.
  Mit Zitat antworten Zitat
Benutzerbild von Dani
Dani

Registriert seit: 19. Jan 2003
732 Beiträge
 
Turbo Delphi für Win32
 
#4

Re: Problem mit Speichern von Combobox-Items in Inifile

  Alt 16. Mär 2004, 19:55
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
Dani H.
  Mit Zitat antworten Zitat
Antwort Antwort


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 13:02 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