Reputation: 629
My application has multiple forms. I load all settings at TForm1.FormCreate (Main Form). I have my configuration panel in form8.
procedure TForm1.FormCreate(Sender: TObject);
begin
settings:=TMemIniFile.Create('');
settings.Create('settings.ini');
if settings.ReadString('settings','ComboBox1','')='1' then
form1.ComboBox1.checked:=true else form1.ComboBox1.checked:=false;
//line below crashes application because form8 has not been initialized yet
if settings.ReadString('settings','ComboBox2','')='1' then
form8.ComboBox1.checked:=true else form8.ComboBox1.checked:=false;
settings.free
end;
Is there any way to force initialization of form8 so I can configure UI elements there? I would really prefer to do that from TForm1.FormCreate. Yes I know that I could load settings from form1.Onshow or form1.Onactivate but this time I need to put code in form1.Oncreate because my application also starts minimized in tray.
Upvotes: 0
Views: 844
Reputation: 7912
Move that settings display code to your settings form (e.g. create method into which you pass the config object). And create and display that settings form only when it's needed. There's no need to have prepared but hidden settings form (not speaking about its possible synchronization when the settings object change).
One idea, not ideal though:
type
TFormConfig = class(TForm)
CheckBoxSomething: TCheckBox;
private
procedure DisplaySettings(ASettings: TMemIniFile);
procedure CollectSettings(ASettings: TMemIniFile);
public
class function Setup(AOwner: TComponent; ASettings: TMemIniFile): Boolean;
end;
implementation
class function TFormConfig.Setup(AOwner: TComponent; ASettings: TMemIniFile): Boolean;
var
Form: TFormConfig;
begin
{ create the form instance }
Form := TFormConfig.Create(AOwner);
try
{ display settings }
Form.DisplaySettings(ASettings);
{ show form and store the result }
Result := Form.ShowModal = mrOK;
{ and collect the settings if the user accepted the dialog }
if Result then
Form.CollectSettings(ASettings);
finally
Form.Free;
end;
end;
procedure TFormConfig.DisplaySettings(ASettings: TMemIniFile);
begin
CheckBoxSomething.Checked := ASettings.ReadBool('Section', 'Ident', True);
end;
procedure TFormConfig.CollectSettings(ASettings: TMemIniFile);
begin
ASettings.WriteBool('Section', 'Ident', CheckBoxSomething.Checked);
end;
And its usage:
if TFormConfig.Setup(Self, Settings) then
begin
{ user accepted the config dialog, update app. behavior if needed }
end;
Upvotes: 2
Reputation: 3503
place your settings code within the DPR:
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
// place settings code here
Application.Run;
end.
Upvotes: -1