VC.NET Question about Forms

I apologize ahead of time for asking a question that may be very simple to answer but I am new to C++ and .NET programming. 

I am trying to create an application that will have 2 forms.  Form1 is the main form of the application and Form2 is the options dialog.  I have tried to search MSDN and Google but have been unsucessful in my attempts (probably due to bad wording of my searches).  I have a button with the following code:

private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
              {
                           Form2::ShowDialog();
              }

but everytime I try to compile the application I get an error message stating:

error C2065: 'Form2' : Undeclared Identifier

I have tried including the Form2.h file and have also tried adding a using namespace Form2 to the Form1.h file but get other errors such as "Cannot find file Form2.h" and "Form2 is not a memeber of namespace WPL".  Form2.h is in the same directory as the rest of the application and WPL is my application name.

Any assistance would be greatly appreciated and if needed I can provide the full source of my application to anyone that needs it.




Answer this question

VC.NET Question about Forms

  • aeonblaire

    At the very least you'll need to throw an include statement at the top of the file you are making this call from ala:

    #include "Form2.h"

    Also very important is that you cannot just call ShowDialog() on the Form2 object as ShowDialog() is not a static method... instead you'll need to create an instance and then call ShowDialog on it ala:

    Form2^ f = gcnew Form2();
    f->ShowDialog();

    Does this work for you



  • SuzanneWeiss

    To prevent something like this from happening in future, I suggest you rename your buttons to something more descriptive than button1, button2, etc. With the names you currently have, it's not going to be the only time you'll get the buttons confused up, and the more buttons you add, the worse it's going to become.

    I know you've already written hundreds of lines of code based on these names, and it's difficult to rename each identifier one by one (particularly with VC2003 and its lack of refactoring support). So what I'll suggest you do is make use of the Find/Replace dialog box. If you need any further help, let me know.

    The same goes for Form1 and Form2, and all the other controls you may wish to add.



  • Martin Bennedik

    hi ,

    Are you using VS.net 2003 or C++.Net for .Net 1.1 . I think Only the Whidbey VS2005 compiler supports the new ' ^ ' way of defining handles..

    Because ur

    Void button1_Click(System::Object * sender, System::EventArgs * e) , ' * ' is used .

    but u r using '^' . Pls check whether its VS2005 /.net 2.0 ,or else u have to use the old C++.net syntax.

    Form2* oForm = new Form2();

    thanks

    vinothkumar



  • ntsoo

    If you minimise the Visual Studio window, you will see that the child dialog was hidden behind it. If you want it so that Form2 appears in front of Visual Studio, use these lines:

    Form2* oForm = new Form2();
    oForm->Show(this); // specify this as the owner form

    BTW why did you switch from the ShowDialog() method to Show()



  • Paulustrious

    I just want to say thank you to all that helped me with this problem. The last part of this was actually a user error. I realized, after OShah pointed out that the button was not enabled, that I have been putting the code to launch the form in the wrong button! I had previously did a test with button24 on the form and was using that onclick event to put my code in to. I apparently overlooked the fact that I had not created an onclick even for button26 which was the button I was clicking and trying to get the form to launch from.... Thank you all for helping me out and I hope to be able to return the favor someday.

  • Erling Ervik

    Ok, the #include "Form2.h" fixed the undeclared identifier issue but now I am getting some new error messages with this code:

    private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
    {
    Form2^ oForm = gcnew Form2();

    oForm->ShowDialog();
    }

    The error messages I get are as follows:

    error C2143: syntax error : missing ';' before '^'
    error C2143: syntax error : missing ';' before '^'
    error C2065: 'oForm' : undeclared identifier
    error C2227: left of '->ShowDialog' must point to class/struct/union type is "unknown-type"



  • Lee Brimelow

    I am using VS .NET 2003 Architect so I will try and use the code you have above when I get home tonight and see if it works.

    Thanks for the reply...



  • Thomas Andersson

    Oh sorry, I was using VC2005 to compile your project and it appears that form2->Show(this) only works in .NET 2.0.

    I noticed that the relevant button was created disabled (or more accurately, its Enabled property was set to false). In order to use the Button, you have to enable it. You can set the Enabled property in the properties window. If you select the button in the Forms Designer, and in the Properties Window, you can set the Enabled property of the button to true.



  • Karthick1020

    Ok, using the * instead of ^ allowed my code to compile completely, but now, when I click the button while the program is running, nothing happens. No errors show up and the form never appears!!! Here is a link to the source code: http://www.sappsworld.com/WPL.zip

    I'd greatly appreciate if someone could tell me what I have done wrong....



  • Greg Brewer

    I didn't really switch it, that was just the last code I tried before zipping up the source. If you do the Show(this) I get an error of "error C2660: 'System::Windows::Forms::Control::Show' : function does not take 1 arguments" and if I do ShowDialog(this) the code compiles but nothing happens when I click the button... I think it may be my computer or maybe I just need to redo the project from scratch... any ideas


  • VC.NET Question about Forms