Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#16

AW: INI File Schreiben und auslesen

  Alt 14. Mär 2014, 09:31
Würde das eher mit einem Aufzählungstypen und über die Eigenschaft ItemIndex machen?
Delphi-Quellcode:
unit MyUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IniFiles;

type
  TLanguage = (lgGerman, lgFrench, lgEnglich, lgSwissGerman);

const
  cLanguage: array [TLanguage] of string =
    ('Deutsch', 'Französisch', 'Englisch', 'Schweizerdeutsch');

type
  TMyForm = class(TForm)
    LanguageComboBox: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure LanguageComboBoxClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    procedure SetLanguageComboBoxItems;
  end;

var
  MyForm: TMyForm;

implementation

{$R *.dfm}

var
  Language: TLanguage;

function LanguageRange(Value: integer): boolean;
var
  A, B: integer;
begin
  A := Integer(Low(TLanguage));
  B := Integer(High(TLanguage));
  Result := (Value >= A) and (Value <= B);
end;

function IntToLanguage(Value: integer): TLanguage;
begin
  if LanguageRange(Value) then
    Result := TLanguage(Value)
  else
    raise Exception.Create('IntToLanguage: Invalid Paramter');
end;

function LanguageToInt(Value: TLanguage): integer;
begin
  Result := Integer(Value);
end;

function IniFileName: string;
var
  Path: string;
begin
  Path := ExtractFilePath(ParamStr(0)) + 'ini\';
  if not DirectoryExists(Path) then
    if not CreateDir(Path) then
      raise Exception.Create(Format('Cannot create folder %s', [Path]));
  Result := Path + 'Einstellungen.ini';
end;

function LoadLanguageFromIniFile: TLanguage;
var
  Ini: TMemIniFile;
begin
  Ini := TMemIniFile.Create(IniFileName);
  try
    Result := IntToLanguage(Ini.ReadInteger('Language', 'Last Language', 0));
  finally
    Ini.Free;
  end;
end;

procedure SaveLanguageToIniFile(Language: TLanguage);
var
  Ini: TMemIniFile;
begin
  Ini := TMemIniFile.Create(IniFileName);
  try
    Ini.WriteInteger('Language', 'Last Language', LanguageToInt(Language));
  finally
    Ini.UpdateFile;
    Ini.Free;
  end;
end;

procedure TMyForm.LanguageComboBoxClick(Sender: TObject);
begin
  if LanguageRange(LanguageComboBox.ItemIndex) then
    Language := IntToLanguage(LanguageComboBox.ItemIndex);
end;

procedure TMyForm.SetLanguageComboBoxItems;
var
  I: TLanguage;
begin
  LanguageComboBox.Items.Clear;
  LanguageComboBox.Style := csDropDownList;
  for I := Low(I) to High(I) do // oder for I := lgGerman to lgSwissGerman do
    LanguageComboBox.Items.Add(cLanguage[I]);
end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  SetLanguageComboBoxItems;
  Language := LoadLanguageFromIniFile;
  LanguageComboBox.ItemIndex := LanguageToInt(Language);
  ShowMessage(cLanguage[Language]);
end;

procedure TMyForm.FormDestroy(Sender: TObject);
begin
  SaveLanguageToIniFile(Language);
end;

procedure DoSomething;
begin
  if Language = lgEnglich then
    ShowMessage('I like Englich');
end;

procedure DoSomethingElse;
begin
  case Language of
    lgGerman:
    begin

    end;
    lgFrench:
    begin

    end;
    lgEnglich:
    begin

    end;
    lgSwissGerman:
    begin

    end;
  end;
end;

end.
  Mit Zitat antworten Zitat