积极答复者
请教:VS2010,VC++.NET中两个Form相互调用问题

问题
-
请教一个问题
用VC++.NET编写Winform程序。工程名为Application
做了两个Form,分别为Form1,Form2
Form1中有个按钮可以进入Form2,Form2中有个按钮可以返回到Form1
应该如何写。我是想这么做的
1. Application.cpp中(就是整个程序main函数所在的那个文件),将其中的 Application::Run(gcnew Form1());
Form1^ forMainInterface = gcnew Form1();
Application::Run(MainInterface);
想的是可以对生成的窗口进行Show、Hide等操作。
2. Form1中用于进入Form2界面的按钮的代码为
Form2^ ParameterSetting = gcnew Form2();
ParameterSetting->Show();
Hide();
3. Form2中用于返回Form1界面的按钮的代码为
forMainInterface->Show();
Close();
可是,步骤1中,为了生成Form1的对象,Application.cpp中就要 include "Form1.h"
步骤2中,为了生成Form2的对象,Form1.h中就要 include "Form2.h"
步骤3中,为了使用对象forMainInterface,Form2.h中就要 include "Application.cpp"
这样就循环include了,好像编译不能通过。- 已编辑 thinkinginworld 2011年10月14日 3:31
答案
-
你好,
根据您的描述,我建议您可以在Form2内加入如下代码:
ref class Form1;
public:Form1 ^ frm1; // 在form2中定义form1,当您在form1中创建一个新的form2时,可以帮form1本身作为参数传入、
在form1中:Form2 ^ frm2 =gcnew Form2(this);
frm2->Show();我在我的skydrive上已经上传了一个名为T2的实例。它实现了如下功能,当点击form1中的按钮,生成form2.同时点击form2的按钮,把字段传回form1. https://skydrive.live.com/P.mvc#!/?cid=ae2442667686801e&sc=documents&uc=1&id=AE2442667686801E%21147
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 thinkinginworld 2011年10月17日 7:14
全部回复
-
你好,
根据您的描述,我建议您可以在Form2内加入如下代码:
ref class Form1;
public:Form1 ^ frm1; // 在form2中定义form1,当您在form1中创建一个新的form2时,可以帮form1本身作为参数传入、
在form1中:Form2 ^ frm2 =gcnew Form2(this);
frm2->Show();我在我的skydrive上已经上传了一个名为T2的实例。它实现了如下功能,当点击form1中的按钮,生成form2.同时点击form2的按钮,把字段传回form1. https://skydrive.live.com/P.mvc#!/?cid=ae2442667686801e&sc=documents&uc=1&id=AE2442667686801E%21147
Rob Pan [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 thinkinginworld 2011年10月17日 7:14