[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Load & Save to ini-file.

Posted on 2009-12-16
15
Medium Priority
?
513 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:peterkiers
  • 7
  • 6
  • 2
15 Comments
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 1600 total points
ID: 26062092
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
 
LVL 12

Assisted Solution

by:Hypo
Hypo earned 400 total points
ID: 26062098
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
 
LVL 1

Author Comment

by:peterkiers
ID: 26062160
with this line:
cbFontName.Items.ItemIndex := cbFontName.Items.IndexOf(ini.ReadString('Font', 'FaceName', Font.Name));
I get an error: Undeclared identifier 'itemindex'.

Peter
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26062200
oops typo
cbFontName.Items.ItemIndex
should be of the combobox, not of the items in the combobox

cbFontName.ItemIndex  := ...
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26062216
Hypo,
I think it will store/load the selected text font/size like that

0
 
LVL 12

Expert Comment

by:Hypo
ID: 26062249
Yeah,
I didn't see your post when I posted my comment...
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26062268
Thanks it works.

Peter
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26062359
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26062477
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
 
LVL 1

Author Comment

by:peterkiers
ID: 26062581
 ??? --> 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
 
LVL 1

Author Comment

by:peterkiers
ID: 26062846
Can someone help me with this last little detail.

Peter.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26063003
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
 
LVL 1

Author Comment

by:peterkiers
ID: 26063152
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 26063211
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
 
LVL 1

Author Comment

by:peterkiers
ID: 26063331
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month20 days, 13 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question