[WinForms] Multiple Forms

So I'm trying to code this program which uses multiple forms. You can switch between them with a button. You can switch back and forth, not only from Form1 to Form2. =3

So in short:
Form1 -> Click on button -> Form2 shows, Form1 hides -> Click on button -> Form1 shows, Form2 hides.

I did it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
namespace MultiForms
{
  ref class Form2;

  public ref class Form1 : public Form
  {
    //Everything not involving Form2 in here
    private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e);
  };

  #include "Form2.h"

  System::Void Form1::button2_Click(System::Object^ sender, System::EventArgs^ e)
  {
    Form2::Visible = true;
  }
}


This returns 2 errors:
1
2
3
4
5
error C2027: use of undefined type 'MultiForms::MultiForms::Form2'
see declaration of 'MultiForms::MultiForms::Form2'

error C2027: use of undefined type 'MultiForms::Form2'
see declaration of 'MultiForms::Form2'


I'm using Microsoft Visual Studio 2008. =3

Thanks in advance. :3
Last edited on
Bumping this. Changed the first post. =3
There is no MultiForms in your namespace, and in your code somewhere you're using MultiForms::MultiForms.

And look carefully at MultiForms::Form2. Doesn't something strike you as wrong about it? Maybe it's a bit short?

-Albatross
EDIT: Oh wait, I think I've found it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include "Form2.h"

ref class Form2;

namespace MultiForms
{
  public ref class Form1 : public Form
  {
    //Everything not involving Form2 in here
    private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e);
  };

  System::Void Form1::button2_Click(System::Object^ sender, System::EventArgs^ e)
  {
    Form1::Visible = false;
    Form2^ NewForm = gcnew Form2;
    NewForm->Visible = true;
  }
}


However, if I do a similar thing in Form2.h, Visual Studio returns these errors in Form1.h:

1
2
3
4
5
6
error C2512: 'Form2' : no appropriate default constructor available

error C2027: use of undefined type 'Form2'
see declaration of 'Form2'

error C2227: left of '->Visible' must point to class/struct/union/generic type


I found that, if I comment out the #include "Form1.h" part along with the button2_Click method body, these errors disappear. In return, I receive this one:

1
2
error LNK2005: "private: void __clrcall MultiForms::Form2::button2_Click(class System::Object ^,class System::EventArgs ^)" (?button2_Click@Form2@MultiForms@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) already defined in MultiForms.obj
fatal error LNK1169: one or more multiply defined symbols found


I found out that it's because of the declaration without a body inside the Form class and because of the declaration WITH body outside the Form class. :\
What should I do?
Last edited on
Topic archived. No new replies allowed.