2012-01-16 7 views
0

フォーム変数の値を別のサブフォームから設定するための「スマートで直接的な」方法とは何ですか? 私は、myVar(ネットワークポート番号など)とsettingsButtonを含むmainFormを持っているとします。これは、myVar ....を設定しようとしている別のフォームsettingsFormを表示します。settingsFormはDialogResult以外のものを返すことができません。 は、私は次のようにそのデータメンバにアクセスするには、コンストラクタにsettingsFormへのMainFormの瞬間を渡すことによって、それを実装するために使用:別のフォームから変数を設定する

//////mainForm.h: 
#include "settingsForm.h" 
... 
ref class mainForm: puplic Form 
{ 
puplic: 
    int myVar; 
private: void settingsButton_Click(Object^ sender, EventArgs^ e) 
{ 
    (gcnew settingsForm(this))->ShowDialog(); 
} 
... 
}; 
.... 
////////settingsForm.h 
... 
ref class mainForm; //forward declaration to avoid circular dependency 
ref class settingsForm:public Form 
{ 
mainForm^ mf; 
settingsForm(mainForm form) 
{ 
    .... 
    mf=form; 
} 
void okButton_click(Object^ sender, EventArgs^ e); //definition in the cpp file 
... 
}; 
///////settingsForm.cpp 
... 
void settingsForm::okButton_click(Object^ sender, EventArgs^ e) 
{ 
    mf->myVar= someValue; 
} 
... 

答えて

0

あなたがそれらの間でデータを交換するイベントや共有領域を使用することができます。 しかし、私はあなた自身の方法がシンプルで良いと思います。 なぜ別の方法をお探しですか?

+0

お返事ありがとうございます! たとえば、settingsFormから値を返すなどして、cppとhファイルの両方を書き直すことを避ける方法があるのだろうかと思います。 – user746277

+0

フィールドの参照をsettingsFormに渡すことができます。このように、フォーム間の依存関係はありませんが、この値を変更するためにイベントを使用する必要があると思います。 –

関連する問題