Re: How do I run Form2, from Form1 ?
On Apr 28, 7:12 am, Keith <Ke...@discussions.microsoft.com> wrote:
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 abo=
ut how
C++ works.
Over in Form2.h=85 I have the following code=85
namespace Test_Forms_Application {
/// <summary>
/// Summary for Form2
///
/// the machine-generated warning text is here=85
///
/// </summary>
public ref class Form2 : public System::Windows::Forms::F=
orm
{
public:
. . . etc=85
The same text is in Form1.h where one of the lines is =85
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=85
// test code B
Test_Forms_Application::Form2^ pForm2 = gcnew Form2();
pForm2->ShowDialog(); // show modal
}
Now the errors are as follows=85
// 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 err=
or
somewhere. When I click on menu shoices =85 =93project=94, and then, =
=93show all
files=94 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.