Load & Save to ini-file.

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.

Open in new window

LVL 1
peterkiersAsked:
Who is Participating?
 
Geert GConnect With a Mentor Oracle dbaCommented:
Peter,

you are storing and saving the form.font.name and size.
you aren't specifying the combobox item selected
procedure TForm1.LoadSettings; 
var ini: TIniFile; 
begin 
  ini := TIniFile.Create(SettingsFile); 
  try 
    cbFontName.Items.ItemIndex := cbFontName.Items.IndexOf(ini.ReadString('Font', 'FaceName', Font.Name)); 
    edtFontSize.Value := ini.ReadInteger('Font', 'PointSize', Font.Size); 
  finally 
    FreeAndNil(ini) 
  end; 
end; 
  
procedure TForm1.SaveSettings; 
var ini: TIniFile; 
begin 
  ini := TIniFile.Create(SettingsFile); 
  try 
    ini.WriteString('Font', 'FaceName', cbFontName.Items[cbFontName.ItemIndex]); 
    ini.WriteInteger('Font', 'PointSize', edtFontSize.Value); 
  finally 
    FreeAndNil(ini) 
  end; 
end; 

Open in new window

0
 
HypoConnect With a Mentor Commented:
Isn't your problem that you store Font.Name and Font.Size to the ini file, which is the Font property of the main form? Since you never change that property it does nothing...

If I understand your code correctly, then you should store CurrText.Name and CurrText.Size to the ini-file, as well as reading CurrText.Name and CurrText.Size from the Inifile...?

/Hypo
procedure TForm1.LoadSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(SettingsFile);
  try
    CurrText.Name := ini.ReadString('Font', 'FaceName', CurrText.Name);
    CurrText.Size := ini.ReadInteger('Font', 'PointSize', CurrText.Size);
  finally
    FreeAndNil(ini)
  end;
end;
 
procedure TForm1.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(SettingsFile);
  try
  ini.WriteString('Font', 'FaceName', CurrText.Name);
  ini.WriteInteger('Font', 'PointSize', CurrText.Size);
  finally
    FreeAndNil(ini)
  end;
end;

Open in new window

0
 
peterkiersAuthor Commented:
with this line:
cbFontName.Items.ItemIndex := cbFontName.Items.IndexOf(ini.ReadString('Font', 'FaceName', Font.Name));
I get an error: Undeclared identifier 'itemindex'.

Peter
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Geert GOracle dbaCommented:
oops typo
cbFontName.Items.ItemIndex
should be of the combobox, not of the items in the combobox

cbFontName.ItemIndex  := ...
0
 
Geert GOracle dbaCommented:
Hypo,
I think it will store/load the selected text font/size like that

0
 
HypoCommented:
Yeah,
I didn't see your post when I posted my comment...
0
 
peterkiersAuthor Commented:
Thanks it works.

Peter
0
 
peterkiersAuthor Commented:
I am trying to do the rest: the combobox for the speech-enigine and the spinedit for the volume and rate.
But I don't get furhter than this.
The problem is what should i put in the place 'here'.  

  cbVoices.ItemIndex := cbVoices.Items.IndexOf(ini.ReadString('Voice','Voice', 'here'));
    VolSpinEdt.Value := ini.ReadInteger('Voice', 'Volume', 'here');
    RateSpinEdt.Value := ini.ReadInteger('Voice', 'Rate', 'here');

P.
0
 
Geert GOracle dbaCommented:
that's a default value
most of the time programmers put constants in
the same value as the defaults for the components

like for font, size:
Arial, 10
or
Times New Roman,12

  ??? --> default of component ?
  cbVoices.ItemIndex := cbVoices.Items.IndexOf(ini.ReadString('Voice','Voice', 'here'));

    VolSpinEdt.Value := ini.ReadInteger('Voice', 'Volume', spVoice.Volumn);
    RateSpinEdt.Value := ini.ReadInteger('Voice', 'Rate', spVoice.Rate);
0
 
peterkiersAuthor Commented:
 ??? --> default of component ?
  cbVoices.ItemIndex := cbVoices.Items.IndexOf(ini.ReadString('Voice','Voice', 'here'));

I don't understand!

What should you put in it?

P.
0
 
peterkiersAuthor Commented:
Can someone help me with this last little detail.

Peter.
0
 
Geert GOracle dbaCommented:
hmmm, i reread your code
you load all those voices in the combobox and set 0 as the default

SpVoice1.EventInterests := SVEAllEvents;
  SOTokens := SpVoice1.GetVoices('', '');
  for I := 0 to SOTokens.Count - 1 do
  begin
    SOToken := SOTokens.Item(I);
*****
--> how you add
    cbVoices.Items.AddObject(SOToken.GetDescription(0), TObject(SOToken));
--->
*****
    SOToken._AddRef;
  end;
  if cbVoices.Items.Count > 0 then
  begin
--> default    
    cbVoices.ItemIndex := 0;
--->
    cbVoices.OnChange(cbVoices);
  end;

so i think it would be like this ...
(you would have to build in a check if you actually have something in the combo)

var defVoice: string;
begin
  defVoice := '';
  if cbVoices.Items.Count > 0 then 
    defVoice := cbVoices.Items[0];
  cbVoices.ItemIndex := cbVoices.Items.IndexOf(ini.ReadString('Voice','Voice', defVoice));

Open in new window

0
 
peterkiersAuthor Commented:
OK what about the SaveSettings procedure:

procedure TForm1.SaveSettings;
var ini: TIniFile;
    defVoice: string;
begin
  ini := TIniFile.Create(SettingsFile);
  try
    ini.WriteString('Font', 'FaceName', cbFontName.Items[cbFontName.ItemIndex]);
    ini.WriteInteger('Font', 'PointSize', edtFontSize.Value);

    ini.WriteString('Voice', 'Voice', defVoice));
    ini.WriteInteger('Voice', 'Volume', SpVoice1.Volume);
    ini.WriteIneger('Voice', 'Rate', SpVoice1.Rate);
  finally
    FreeAndNil(ini)
  end;
end;
0
 
Geert GOracle dbaCommented:
if you save something then you should take the actual value of the component
you want to store the value of

looks good except the defvoice is not filled in.

defVoice := '';
if  cbVoices.ItemIndex >= 0 then
  defVoice := cbVoices.Items[cbVoices.ItemIndex];
ini.WriteString('Voice', 'Voice', defVoice));

you have a typo too  ... :)


WriteInteger
       ^

Open in new window

0
 
peterkiersAuthor Commented:
I have test it and everything works except the combobox cbVoices.
Because when i choose Samantha in the combobox and volume 77
and close the application. Then I look in the inifile and see Samantha
+ 77 written. So the writing part is OK. But when I load the app again
in the combobox cbVoices I see Microsoft Sam instead of samantha.

procedure TForm1.LoadSettings;
var ini: TIniFile;
    defVoice: string;
begin
  ini := TIniFile.Create(SettingsFile);
  try
   defVoice := '';
   if cbVoices.Items.Count > 0 then
   defVoice := cbVoices.Items[0];
   cbFontName.ItemIndex  := cbFontName.Items.IndexOf(ini.ReadString('Font', 'FaceName', Font.Name));
   edtFontSize.Value := ini.ReadInteger('Font', 'PointSize', Font.Size);
   cbVoices.ItemIndex := cbVoices.Items.IndexOf(ini.ReadString('Voice','Voice', defVoice));
   VolSpinEdt.Value := ini.ReadInteger('Voice', 'Volume', SpVoice1.Volume);
   RateSpinEdt.Value := ini.ReadInteger('Voice', 'Rate', SpVoice1.Rate);
  finally
    FreeAndNil(ini)
  end;  
end;

procedure TForm1.SaveSettings;
var ini: TIniFile;
    defVoice: string;
begin
  ini := TIniFile.Create(SettingsFile);
  try
  defVoice := '';
  if cbVoices.ItemIndex >= 0 then
  defVoice := cbVoices.Items[cbVoices.ItemIndex];
    ini.WriteString('Font', 'FaceName', cbFontName.Items[cbFontName.ItemIndex]);
    ini.WriteInteger('Font', 'PointSize', edtFontSize.Value);
    ini.WriteString('Voice', 'Voice', defVoice);
    ini.WriteInteger('Voice', 'Volume', SpVoice1.Volume);
    ini.WriteInteger('Voice', 'Rate', SpVoice1.Rate);
  finally
    FreeAndNil(ini)
  end;
end;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.