Display a CLI form from a CLI form

I know this should be the simplest thing to do, yet I have gotten nowhere spending several hours in Horton's book, MSDN help, and internet searches.

I am using VC++ 2008 express and trying to simply open a windows form I made in the editor from a button in Form1.

In the Form1.h file, I have:

#include "Form2.h" // at the top above namespace FormTesting {

InitializeComponent();
//
//TODO: Add the constructor code here
//
Form2^ form2 = gcnew Form2();

private: System::Void btnForm2_Click(System::Object^ sender, System::EventArgs^ e) {

form2->ShowDialog ();
}
};
}


I get error C2065: 'form2' : undeclared identifier. My attempts that did produce a form gave me a blank rather than one matching my efforts in the form editor.
I like you am starting out on Visual C++ and have had the same Problem.
I have multiple windows in the program I am writing that I want to call to action with buttons. The best I could come up with with hours of looking is a way to press the button and the second window will load in front of the first. The second can be interacted with, used and closed, and you have the first window again. I have not figured out how to successfully unload the first window on the loading of the second. but with this example I can load and use the second window.

This is example loads a second window from a form named ManageDbase.h . The following is the code for the button to load the second window:

private: System::Void btnInvMgr_Click(System::Object^ sender, System::EventArgs^ e) {

ManageDbase^ manage = gcnew ManageDbase();
manage->Show();
}


This code assumes that you have: #include "ManageDbase.h"
at the top of Form1's codeing page.

From what I understand is this is creating a new pointer to the form ManageDbase.h and creating an instance of it called manage. And the next line shows the window.

Hope this helps,
Raylon00
The .h file is just a header which contains the class definition for the form, not the form itself.

The method I have been using is to declare an instance of the form I wish to create as a private member of the form class I wish to create it from
EG
1
2
3
4
5
public ref class MainForm : public System::Windows::Forms::Form
{
private:
   OptionsEditor ^optionsEditor;  //OptionsEditor is the class for the form I want to appaear
...

then in the Click event I use similar code to Rayloo00
1
2
3
4
5
6
7
8
9
System::Void MainForm::optionsToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
{
   optionsEditor = gcnew OptionsEditor(options);  //options is a class with info I need to pass to the new form
   optionsEditor->ShowDialog(); 
   if (options->changed)
   {
      options->Save();
   }
}

Using ShowDialog() rather than Show() prevents the user from clicking back on the original form, and also causes the original form to wait until teh new form is closed to continue.
This is important as otherwise the check for options->changed would occur before the user had a chance to interact with the new form!

I was building a main + popup design, so did not need (or want) the main form to disapear, but a quick look at the docs shows there is a Hide() method, so something like
1
2
3
4
5
6
7
System::Void MainForm::optionsToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
{
   Hide();  //Hide Main Form;
   optionsEditor = gcnew OptionsEditor(options);
   optionsEditor->ShowDialog();
   Show(); //Make Main Form reapear;
}

should do what you want.
Last edited on
Topic archived. No new replies allowed.