troubleshooting Question

Load & Save to ini-file.

Avatar of Peter Kiers
Peter KiersFlag for Netherlands asked on
Delphi
15 Comments2 Solutions560 ViewsLast Modified:
Dear Experts,

I have a little example with a dbrichedit1 and a toolbar with 2 comboxes and 3 spinedits.
1 combobox is for choosing a fontname and 1 spinedit for choosing the fontsize.
Then I have 1 combox for choosing a text-to-speech engine, and 2 spinedits are for adjust
its speech-volume and rate. Everthing works great.

But now I am trying to save and load the selections of the 2 comboboxes and the 3 spinedits
to a ini-file. But it doesn't work! And I don't know what I do wrong. Does someone knows the
answer and is willing to help me? I have put the code in the code section.

Greetings, Peter Kiers
type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    StatusBar1: TStatusBar;
    ToolBar1: TToolBar;
    cbFontName: TComboBox;
    DBRichEdit1: TDBRichEdit;
    edtFontSize: TSpinEdit;
    cbVoices: TComboBox;
    RateSpinEdt: TSpinEdit;
    VolSpinEdt: TSpinEdit;
    SpVoice1: TSpVoice;
    procedure RateSpinEdtChange(Sender: TObject);
    procedure VolSpinEdtChange(Sender: TObject);
    procedure cbVoicesChange(Sender: TObject);
    procedure edtFontSizeChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cbFontNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    SettingsFile: string;  
    function CurrText: TTextAttributes;
    procedure GetFontNames;
    procedure LoadSettings;
    procedure SaveSettings;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
  FontType: Integer; Data: Pointer): Integer; stdcall;
begin
  TStrings(Data).Add(LogFont.lfFaceName);
  Result := 1;
end;
 
procedure TForm1.cbFontNameChange(Sender: TObject);
begin
  CurrText.Name := cbFontName.Items[cbFontName.ItemIndex];
end;
 
procedure TForm1.cbVoicesChange(Sender: TObject);
var
  SOToken: ISpeechObjectToken;
begin
  SOToken := ISpeechObjectToken(Pointer(
    cbVoices.Items.Objects[cbVoices.ItemIndex]));
  SpVoice1.Voice := SOToken;
end;
 
function TForm1.CurrText: TTextAttributes;
begin
  if DBRichEdit1.SelLength > 0 then Result := DBRichEdit1.SelAttributes
  else Result := DBRichEdit1.DefAttributes;
end;
 
procedure TForm1.edtFontSizeChange(Sender: TObject);
begin
  CurrText.Size := StrToInt(edtFontSize.Text);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  SOToken: ISpeechObjectToken;
  SOTokens: ISpeechObjectTokens;
begin
 GetFontNames;
 SettingsFile := ChangeFileExt(Application.ExeName, '.ini');
 LoadSettings;
 
 
  SpVoice1.EventInterests := SVEAllEvents;
  SOTokens := SpVoice1.GetVoices('', '');
  for I := 0 to SOTokens.Count - 1 do
  begin
    SOToken := SOTokens.Item(I);
    cbVoices.Items.AddObject(SOToken.GetDescription(0), TObject(SOToken));
    SOToken._AddRef;
  end;
  if cbVoices.Items.Count > 0 then
  begin
    cbVoices.ItemIndex := 0;
    cbVoices.OnChange(cbVoices);
  end;
  RateSpinEdt.Value := SpVoice1.Rate;
  VolSpinEdt.Value:= SpVoice1.Volume;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
var
  I: Integer;
begin
 for I := 0 to cbVoices.Items.Count - 1 do
 ISpeechObjectToken(Pointer(cbVoices.Items.Objects[I]))._Release;
 SaveSettings;
end;
 
procedure TForm1.GetFontNames;
var
  DC: HDC;
begin
  DC := GetDC(0);
  EnumFonts(DC, nil, @EnumFontsProc, Pointer(cbFontName.Items));
  ReleaseDC(0, DC);
  cbFontName.Sorted := True;
end;
 
procedure TForm1.LoadSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(SettingsFile);
  try
    Font.Name := ini.ReadString('Font', 'FaceName', Font.Name);
    Font.Size := ini.ReadInteger('Font', 'PointSize', Font.Size);
  finally
    FreeAndNil(ini)
  end;
end;
 
procedure TForm1.RateSpinEdtChange(Sender: TObject);
begin
  SpVoice1.Rate :=RateSpinEdt.Value;
end;
 
procedure TForm1.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(SettingsFile);
  try
  ini.WriteString('Font', 'FaceName', Font.Name);
  ini.WriteInteger('Font', 'PointSize', Font.Size);
  finally
    FreeAndNil(ini)
  end;
end;
 
procedure TForm1.VolSpinEdtChange(Sender: TObject);
begin
  SpVoice1.Volume := VolSpinEdt.Value;
end;
 
end.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 15 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros