2017-06-17 7 views
0

私のアプリケーションには複数のフォームがあります。 TForm1.FormCreate(メインフォーム)ですべての設定を読み込みます。 form8に私の設定パネルがあります。TForm1.FormCreateから別のフォームを初期化するには?

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; 

form8の初期化を強制する方法はありますか?そこでUI要素を設定できますか?私は本当にTForm1.FormCreateからそれを行うことを好むでしょう。はい、私は私が私のアプリケーションもトレイで最小限に抑えられるので、form1.Onshowまたはform1.Onactivateから設定をロードできることを知っていますが、今度はform1.Oncreateにコードを入れる必要があります。 DPR内

+0

は、必要な情報を受け取るコンストラクタを宣言します。 –

+0

実際、メインフォームを他のフォームの表示を管理するのではなく、それ以外の方法で表示します。他のフォームは*メインフォームから*読みます。あるいは、アプリケーション全体のためにグローバルに共有されているバックグラウンドのオブジェクトから、より優れています。 –

+0

TForm1.FormCreateのform1への参照は悪い習慣です - 'ComboBox1.Checked:= True'を使用してください。これは、特定のインスタンスではなく、常にTForm1の現在のインスタンスを参照します。また、ブロック全体を 'ComboBox1.Checked:= settings.ReadString( 'settings'、 'C​​omboBox1'、 '')= '1' 'に置き換えることもできます –

答えて

-1

場所、設定コード:あなたの設定フォームに表示コードを設定

Application.Initialize; 
    Application.MainFormOnTaskbar := True; 
    Application.CreateForm(TForm1, Form1); 
    Application.CreateForm(TForm2, Form2); 

    // place settings code here 

    Application.Run; 
end. 
2

移動(例えば、あなたがconfigオブジェクトを渡す先のメソッドを作成します)。必要なときにのみ設定フォームを作成して表示します。準備ができているが設定されていない設定フォームを用意する必要はありません(設定オブジェクトが変更されたときの同期の可能性については言及していません)。

ひとつのアイデア、ない理想的なかかわら:

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; 

とその使用方法:

if TFormConfig.Setup(Self, Settings) then 
begin 
    { user accepted the config dialog, update app. behavior if needed } 
end; 
関連する問題