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

x
?
Solved

Load & Save to ini-file, part3

Posted on 2009-12-17
6
Medium Priority
?
364 Views
Last Modified: 2012-05-08
Hi,

I have an example with 2 comboboxes and 3 spinedits on it.
The code that I have put in the code-section loads and saves
the settings from these components to an ini-file.

Everything works great, except for the combobox cbVoices.
Who can solve this problem?

Peter
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;
    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.RateSpinEdtChange(Sender: TObject);
begin
  SpVoice1.Rate :=RateSpinEdt.Value;
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;

procedure TForm1.VolSpinEdtChange(Sender: TObject);
begin
  SpVoice1.Volume := VolSpinEdt.Value;
end;

end.

Open in new window

0
Comment
Question by:peterkiers
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 26071929
hum. It should work as you are using ItemIndex and IndexOf for this, which is necessary if Style=csDropDownList
what does it do ? errors, or the Ini file is not saved as it should or it is not loaded correctly ?
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26072019
The problem lies only with the combobox cbVoices.
This is a combobox for choosing speech-engines. I have 3 voices:
1. Samantha (english)
2. Claire (Dutch)
3. Microsoft SAM (default in Windows)

When I select for example Claire and close my application.
I look in the ini-file and I see Claire is written.
But then I open my application again
And the combobox cbVoices gives me Microsoft SAM.

P.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 26072085
you call your LoadSettings after getting all the voices...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 26072097
I mean you presently call it before, you have to call it after
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26072166
Oh I get it. Now it works .

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  SOToken: ISpeechObjectToken;
  SOTokens: ISpeechObjectTokens;
begin
 GetFontNames;
 SettingsFile := ChangeFileExt(Application.ExeName, '.ini');
  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;
  LoadSettings;
end;

Peter Kiers
0
 
LVL 1

Author Comment

by:peterkiers
ID: 26072240
500 points are comming to you...


Greetings,

Peter Kiers
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 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