Discussion:
How do I run Form2, from Form1 ?
(too old to reply)
Keith
2010-04-23 04:39:01 UTC
Permalink
Hello,

I have a C++ Express 2008 program with two forms, Form1, and Form2.

Form1 has a button, Button2, which has the method shown below. I would like
to know what code needs to go into the method to run and show, Form2.

private: System::Void button3_Click(System::Object^ sender,
System::EventArgs^ e)
{
// what goes here to open and show Form2 ?

}


Thank you,

Keith
Faisal
2010-04-23 05:35:24 UTC
Permalink
Post by Keith
Hello,
I have a C++ Express 2008 program with two forms,   Form1, and Form2.
Form1 has a button,  Button2, which has the method shown below. I would like
to know what code needs to go into the method to run and show, Form2.
private: System::Void button3_Click(System::Object^  sender,
System::EventArgs^  e)
   {
     // what goes here to open and show Form2 ?        
   }
Thank you,
Keith
Just create the Form2 and show it

Form2^ pForm2 = gcnew Form2();

pForm2 ->ShowDialog(); For showing as modal

or pForm2 ->Show() //non-modal
Keith
2010-04-23 12:23:01 UTC
Permalink
Hello Faisal,
Thank you very much.
Keith
Keith
2010-04-24 02:45:01 UTC
Permalink
Hello,

I appreciate the assistance. I used the text shown below, and received an
“undeclared identifier” error. What have I missed?

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

Form2^ pForm2 = gcnew Form2();//error C2065: 'Form2' : undeclared identifier
pForm2->ShowDialog(); // show modal
//Form2a->Show(); // show non-modal

}

Keith
Faisal
2010-04-26 03:41:38 UTC
Permalink
Post by Keith
Hello,
I appreciate the assistance.  I used the text shown below, and received an
“undeclared identifier” error.  What have I missed?
private: System::Void button3_Click(System::Object^  sender,
System::EventArgs^  e) {
Form2^ pForm2 = gcnew Form2();//error C2065: 'Form2' : undeclared identifier
pForm2->ShowDialog();  // show modal
//Form2a->Show(); // show non-modal
}
Keith  
You need to include the Form2 declared file in your .cpp file. If
Form2 is defined in form2.h
use
#include <form2.h>
Keith
2010-04-27 00:34:01 UTC
Permalink
Thanks very much for continuing to answer my questions.

I tried a couple of options and included the following two lines...

#include "Form1.h"
#include <Form2.h> // also tried.... #include "Form2.h"

and both ended up with the same error. I must have something else wrong.

Keith
Faisal
2010-04-27 03:55:54 UTC
Permalink
Post by Keith
Thanks very much for continuing to answer my questions.
I tried a couple of  options and included the following two lines...
#include "Form1.h"
#include <Form2.h> // also tried....   #include "Form2.h"
and both ended up with the same error.  I must have something else wrong.
Keith
Check whether the Form2 class defined in a different namespace. If so,
use full namespace qualifier when using Form2 class.
like
Form2Namespace::Form2^ form2 = gcnew Form2Namespace::Form2;

or add
using Form2Namespace;
Form2^ = gcnew Form2;
Keith
2010-04-28 02:12:01 UTC
Permalink
Thank you for your comments and thank you for continuing to help.
I built this application to try to learn more about C++ so, least for me,
it’s become a bit of an interesting quest that’s telling me a lot about how
C++ works.

Over in Form2.h… I have the following code…

namespace Test_Forms_Application {

/// <summary>
/// Summary for Form2
///
/// the machine-generated warning text is here…
///
/// </summary>

public ref class Form2 : public System::Windows::Forms::Form
{
public:

. . . etc…


The same text is in Form1.h where one of the lines is …

namespace Test_Forms_Application {

with code for Form1 below that.



So I tried both of the following code items in the
Onclick method for the button in

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


/*
// test code A
using Test_Forms_Application;
Form2^ pForm2 = gcnew Form2();
pForm2->ShowDialog(); // show modal
*/


// or…

// test code B
Test_Forms_Application::Form2^ pForm2 = gcnew Form2();
pForm2->ShowDialog(); // show modal

}



Now the errors are as follows…


// error for test code A
error C2873: 'Test_Forms_Application' : symbol cannot be used in a
using-declaration

// error for test code B
error C2039: 'Form2' : is not a member of 'Test_Forms_Application'


and that just doesn’t make sense, so I’ve clearly made some other error
somewhere. When I click on menu shoices … “project”, and then, “show all
files” I see tabs in the workspace that show both Form1.h and form2.h. Is it
possible that somehow Form2 is not part of the project and I’m clearly not
seeing it?


Keith
Faisal
2010-04-28 03:48:48 UTC
Permalink
Post by Keith
Thank you for your comments and thank you for continuing to help.
I built this application to try to learn more about C++ so,  least for me,
it’s become a bit of an interesting quest that’s telling me a lot about how
C++ works.
Over in Form2.h… I have the following code…
namespace Test_Forms_Application {
        /// <summary>
        /// Summary for Form2
        ///
        /// the machine-generated warning text is here…
        ///
/// </summary>
        public ref class Form2 : public System::Windows::Forms::Form
        {
                . . . etc…
The same text is in Form1.h where one of the lines is …
namespace Test_Forms_Application {
        with code for Form1 below that.
So I tried both of the following code items in the
Onclick method for the button in
private: System::Void button3_Click(System::Object^  sender,
System::EventArgs^  e) {
                /*
                // test code A
                using Test_Forms_Application;
                Form2^ pForm2 = gcnew Form2();
                pForm2->ShowDialog();  // show modal
                */
// or…        
                // test code B
Test_Forms_Application::Form2^ pForm2 = gcnew Form2();
                pForm2->ShowDialog();  // show modal
                 }
Now the errors are as follows…
// error for test code A
error C2873: 'Test_Forms_Application' : symbol cannot be used in a
using-declaration
// error for test code B
error C2039: 'Form2' : is not a member of 'Test_Forms_Application'
and that just doesn’t make sense, so I’ve clearly made some other error
somewhere.  When I click on menu shoices … “project”, and then, “show all
files” I see tabs in the workspace that show both Form1.h and form2.h.  Is it
possible that somehow Form2 is not part of the project and I’m clearly not
seeing it?  
Keith
In this case both the Forms are in same namespace, So that there is no
need for specifying the namespace.

The below code should be sufficient

#include "Form2.h"

private: System::Void button3_Click(System::Object^ sender,
System::EventArgs^ e)
{
Form2^ pForm2 = gcnew Form2();
pForm2->ShowDialog();
}

If this code shows error( as you specified in the original post ),
check whether the file Form2.h is included. If the file is not in the
same folder as form1.h you have to specify the relative path. Normally
when the file inclusion fails compiler shows
fatal error C1083: Cannot open include file: filename.xxx

I can't find any other reason for "undeclared identifier" error.
Keith
2010-04-30 02:46:01 UTC
Permalink
Thank you very much.
It's going to take me a day or so to test this one.
Keith
Keith
2010-04-30 12:09:01 UTC
Permalink
Hi Faisal,

I sincerely appreciate your help. I've probably made a mistake that isn't
revealing itself yet. I think I'm going to set the problem aside and work
on some other areas for awhile ... maybe recreate the whole project to see if
this issue continues to exist. I just bought Visual Studio, so it will be a
perfect time to recreate it in that new platform after the disks arrive.

Thanks so very much.

Keith

Loading...