Winforms C++/Cli将数据传递到子窗体(并返回)或如何将对象传递到子窗体
我是从C++\Cli开始的,所以要温柔;-) 我的问题似乎并不罕见,但我一直未能找到一个适合我的解决方案,即我能够实施的解决方案 我在Form1_Load()中创建了一个对象“Genome”,然后我想在通过工具条菜单显示的子Form3中提供数据。在构造函数中为对象提供了默认系统数据。此默认数据需要作为输入字段中的起始值发送到子表单。用户接受或修改此数据后,需要相应地修改对象,然后关闭子窗体 我的感觉是,这可以通过向子窗体发送对象的指针/句柄来实现,从而使数据可以访问 两个问题:1)基本,2)不太基本 1) ToolstripMenuItem\u单击操作未看到在Form1\u加载中创建的对象 2) 我似乎不知道如何使用ToolStripMenuItem\u click事件将(句柄)传递给对象,以便在子窗体中访问对象数据 下面是一些代码,我已经尽可能地精简了,我希望不要太多(数据隐藏仍然需要做): 基因组Winforms C++/Cli将数据传递到子窗体(并返回)或如何将对象传递到子窗体,winforms,c++-cli,Winforms,C++ Cli,我是从C++\Cli开始的,所以要温柔;-) 我的问题似乎并不罕见,但我一直未能找到一个适合我的解决方案,即我能够实施的解决方案 我在Form1_Load()中创建了一个对象“Genome”,然后我想在通过工具条菜单显示的子Form3中提供数据。在构造函数中为对象提供了默认系统数据。此默认数据需要作为输入字段中的起始值发送到子表单。用户接受或修改此数据后,需要相应地修改对象,然后关闭子窗体 我的感觉是,这可以通过向子窗体发送对象的指针/句柄来实现,从而使数据可以访问 两个问题:1)基本,2)不太
ref class Genome
{
public:
int nTFA; // nr of Transcription Factor genes
int nFDE; // nr of Function cell Death genes
List<char>^ cString; // chromosome string
List<Gene^>^ cStruct; // structure consisting of a List with Gene objects
// methods
void loadIniGen(String^ iniFile); // load genome values from .ini file
Genome(void);
};
谢谢,简
让我们添加错误:
1) 给出:错误C2065:“myGenome”:未声明的标识符
2) 给出:错误C3673:“GenomeV2::Form3”:类没有复制构造函数(当我在ToolStrip操作中创建myGenome对象进行测试时)
为了澄清,Form3已经设计好了,但还没有任何功能。总结:
namespace GenomeV2 {
public ref class Form3 : public System::Windows::Forms::Form
{
public:
Form3(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form3()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::GroupBox^ groupBox1;
etc.
#pragma region Windows Form Designer generated code
etc.
#pragma endregion
private: System::Void Form3_Load(System::Object^ sender, System::EventArgs^ e)
{
numericUpDown1->Minimum = 10; // TFA nr of Transcription Factor genes
numericUpDown1->Maximum = 100;
etc.
}
名称空间GenomeV2{
public ref类Form3:public System::Windows::Forms::Form
{
公众:
表格3(无效)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
///
///清理所有正在使用的资源。
///
~Form3()
{
if(组件)
{
删除组件;
}
}
private:System::Windows::Forms::GroupBox^groupBox1;
等
#pragma区域Windows窗体设计器生成的代码
等
#布拉格端区
private:System::Void Form3_Load(System::Object^sender,System::EventArgs^e)
{
numericUpDown1->最小值=10;//转录因子基因的TFA nr
numericUpDown1->最大值=100;
等
}
myGenome
对象存储在其他方法可以看到的地方。现在,它是Form1\u Load
方法中的局部变量。将其设为类字段,该对象中的其他方法可以看到它
ref class Genome
{
public:
Genome^ myGenome;
};
表单3
添加一个构造函数,该构造函数接受基因组^
。完成此操作后,您对myGenome
的存储进行了上述更改,则应编译此行genomeToolStripMenuItem\u单击ShowDialog
返回后的
好吧,我要让它工作。无论这是正确的方法,还是可以改进的方法,请告诉我 1) 使
myGenome
可见
我已经将Genome^myGenome;
的变量声明放在Form1
公共声明中,这样它对所有操作都是可见的,然后我在Form1\u Load
事件中使用myGenome=gcnew Genome();
这可以稍后更改为包含基因组对象的列表等
2) 在表单之间来回传递数据
按照David的建议,当使用Form3^ genomeForm=gcnewform3(myGenome)实例化genomeForm时,我将(现在可见的)myGenome
对象传递到Form3
。在Form3
声明中,我创建了一个bGenome
对象作为副本,以获取传递到表单的数据。这是在Form3
的构造函数中完成的(请参见下面的代码)。此bGenome
对象包含Form1的myGenome
对象的数据副本,可用于填充Form3
的数值上下字段,即从Form1
到Form3
的数据传输
当检查所有数据并根据需要修改时,表单将以OK关闭。此时,在numericUpDown字段中输入的数据将复制回bGenome
对象
由于表格3
包含在表格1
中(表格1
未包含在表格3
中,以避免循环引用),Form1
可以访问bGenome
,并且bGenome
的数据可以复制回myGenome
,关闭循环:数据从Form3
传输回Form1
注意:我意识到Form1
也可以读取Form3
的数字上下字段,因为它有访问权限,但我认为将这些字段保留在Form3
中,只反馈一个完整的修改对象bGenome
,然后可以在一个语句中复制到myGenome
对象myGenome
用于向用户填充输入字段,并且(修改后的)输入反馈到myGenome
代码,所有非必需品都脱光了
表格1.h
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
Genome^ myGenome = gcnew Genome(); // instantiate genome object
}
private: System::Void genomeToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
Form3^ genomeForm = gcnew Form3(myGenome); // pass genome object to Form3 for data input
genomeForm->Text = "Genome settings";
genomeForm->StartPosition = FormStartPosition::CenterParent;
genomeForm->ShowDialog(); // modal
}
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Genome^ myGenome;
etc.
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
myGenome = gcnew Genome(); // instantiate genome object
}
etc.
private: System::Void genomeToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
Form3^ genomeForm = gcnew Form3(myGenome); // pass genome object to Form3 for data input
genomeForm->ShowDialog(); // modal
myGenome->nTFA = genomeForm->bGenome->nTFA; // feed data from Form3 back to myGenome
textBox1->Text = myGenome->nTFA.ToString();
}
表格3.h
public ref class Form3 : public System::Windows::Forms::Form
{
public:
Genome^ bGenome;
public:
Form3(Genome^ aGenome)
{
InitializeComponent();
bGenome = gcnew Genome();
bGenome = aGenome;
}
etc.
private: System::Void Form3_Load(System::Object^ sender, System::EventArgs^ e)
{
numericUpDown1->Minimum = 10; // TFA nr of Transcription Factor genes
etc.
numericUpDown1->Value = bGenome->nTFA; // show the current Genome data in the input field
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
bGenome->nTFA = Convert::ToInt32(numericUpDown1->Value); // copy the (modified) input data back into the Genome object
etc.
this->DialogResult = System::Windows::Forms::DialogResult::OK;
this->Close();
}
我希望能够消除本地拷贝(aGenome
和bGenome
),并直接从Form3
访问myGenome
,因此如果有办法,请让我知道。我感觉这可以通过将Form1
传递到Form3
和#include>来实现
public ref class Form3 : public System::Windows::Forms::Form
{
public:
Genome^ bGenome;
public:
Form3(Genome^ aGenome)
{
InitializeComponent();
bGenome = gcnew Genome();
bGenome = aGenome;
}
etc.
private: System::Void Form3_Load(System::Object^ sender, System::EventArgs^ e)
{
numericUpDown1->Minimum = 10; // TFA nr of Transcription Factor genes
etc.
numericUpDown1->Value = bGenome->nTFA; // show the current Genome data in the input field
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
bGenome->nTFA = Convert::ToInt32(numericUpDown1->Value); // copy the (modified) input data back into the Genome object
etc.
this->DialogResult = System::Windows::Forms::DialogResult::OK;
this->Close();
}