2012-01-16 97 views
0

什么是从另一个子表单设置某些表单变量值的“智能和直接”方式。 说我有mainForm包含myVar(例如网络端口号)和settingsButton,当单击时显示另一个表单settingsForm,旨在设置myVar .... 因为settingsForm不能返回除DialogResult以外的东西,我该如何实现它。 我曾经通过将MainForm的瞬间settingsForm在其构造如下访问其数据成员来实现它:从另一个表单设置变量

//////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 2012-01-16 14:37:29

+0

您可以将您的字段的引用传递给settingsForm,这样,表单之间不存在依赖关系,但我认为您必须使用事件来更改此值,这样会更好。 – 2012-01-17 08:08:45

相关问题