Einzelnen Beitrag anzeigen

Popov
(Gast)

n/a Beiträge
 
#15

AW: INI File Schreiben und auslesen

  Alt 14. Mär 2014, 07:48
Klein wenig übertrieben der Code, aber es soll ja ein Beispiel sein:
Delphi-Quellcode:
uses
  IniFiles;

const
  LangList = 'Deutsch, Englisch, Französisch, Spanisch'; //Mit Komma getrennte Liste
  IniLangSection = 'Sprachen';
  IniLangList = 'ListeSprachen';
  IniCurLang = 'AktuelleSprache';

function GetIniPath(var IniPath: String): Boolean;
const
  IniSubFolder = 'Ini\';
  IniFileName = 'Einstellungen.ini';
var
  Path: String;
begin
  Path := ExtractFilePath(ParamStr(0));
  IniPath := Path + IniSubFolder + IniFileName;

  Result := ForceDirectories(Path + IniSubFolder);
  if not Result then
    MessageDlg('Fehler. Es konnte kein Ini Ordner angelegt werden.', mtError, [mbOK], 0);
end;

procedure LoadFromIni(ComboBox: TComboBox);
var
  IniFile: TIniFile;
  IniPath: String;
  s: String;
  k: Integer;
begin
  if not GetIniPath(IniPath) then Exit;

  IniFile := TIniFile.Create(IniPath);
  try
    s := IniFile.ReadString(IniLangSection, IniLangList, LangList);
    ComboBox.Items.CommaText := s;
    k := IniFile.ReadInteger(IniLangSection, IniCurLang, 0);
    if (k > -1) and (k < ComboBox.Items.Count) then
      ComboBox.ItemIndex := k;
  finally
    IniFile.Free;
  end;
end;

procedure SaveToIni(ComboBox: TComboBox);
var
  IniFile: TIniFile;
  IniPath: String;
  s: String;
begin
  if not GetIniPath(IniPath) then Exit;

  IniFile := TIniFile.Create(IniPath);
  try
    IniFile.WriteString(IniLangSection, IniLangList, ComboBox.Items.CommaText);
    IniFile.WriteInteger(IniLangSection, IniCurLang, ComboBox.ItemIndex);
  finally
    IniFile.Free;
  end;
end;

procedure AddLanguage(ComboBox: TComboBox; Name: String);
begin
  ComboBox.Items.Add(Name);
end;

procedure TForm1.ButtonLoadClick(Sender: TObject);
begin
  LoadFromIni(ComboBox1);
end;

procedure TForm1.ButtonSaveClick(Sender: TObject);
begin
  SaveToIni(ComboBox1);
end;

procedure TForm1.ButtonAddClick(Sender: TObject);
var
  s: String;
begin
  s := InputBox('Sprache', 'Neue Sprache hinzufügen', '');
  if Trim(s) <> 'then
    AddLanguage(ComboBox1, s);
end;

Geändert von Popov (14. Mär 2014 um 07:53 Uhr)
  Mit Zitat antworten Zitat