Multiple Forms

Hi there,

I understand that there are a number of discussions about this, some of which have been solved, but I can't seem to get my head around this issue or understand these discussions.

This is what I want to do:
Two forms named Form1 and Form2.Form1 has a button which hides Form1 and loads Form2. Form2 has a button which hides Form2 and loads Form1. Simple!

But I have errors. This is what I've coded:

Form1.h has #include "Form2.h" at the beginning of its code. For its button its code is:
1
2
3
4
5
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->Hide();
				 Form2^ form2 = gcnew Form2();
				 form2->ShowDialog();
			 }


Note: Without adding code to Form2.h the above works without error.Obviously, Form2's button is inactive.

Form2.h has #include "Form1.h" at the beginning of its code. For its button, its code is:
1
2
3
4
5
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->Hide();
				 Form1^ form1 = gcnew Form1();
				 form1->ShowDialog();
			 }


I think I understand that it has something to do with the order of #includes in each header. I may be wrong.

If it is a simple task, could someone please provide me with a step - by - step
guide on what to do? I would really appreciate this.

Thanks in advance.
Last edited on
Are these #includes happening in the source files? If not, you may be having a circular inclusion issue. The following may be helpful:
http://www.cplusplus.com/forum/articles/10627/
Thanks for the reply, Zhuge.

This is the coding for the cpp files.

Form1.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"
#include "Form1.h"

using namespace Form1;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew ::Form1::Form1());
	return 0;
}


Form2.cpp:
1
2
#include "StdAfx.h"
#include "Form2.h" 


Thanks for the link. I will read through it now.

Cheers,
Hi,

After searching / reading other posts and websites, I came across this page which solves the above question I posted a little while ago. For anyone who is interested, check out:

http://mcn-www.jwu.ac.jp/~yokamoto/openwww/vsg/VCpp2012FormApp/

I'm using vc++/clr 2010. I couldn't open the project file for the second half of the tutorial (written for vc++/clr 2012). I was able to open each *.cpp and *.h file, copy code into a *.txt file then transfer it into my version of the program with minor editing.

I hope this helps fellow newbies to programming...
Topic archived. No new replies allowed.