Hey,
i have a CLR proyect where i have 2 forms, i am trying to acces the form1 class via form2.

what i did is that i included the header.h of the form in form.h but it failed so i went back and icluded thesame folder both of them are there and then it compiled fine.
then i tried to acces form1.h but it doesnt locate anything in the header so then i made a function in form1.h and not even that it wont located it wont locate.

Dani AI

Generated

As described, this is usually a symbol-resolution or project-configuration problem rather than a mysterious Visual Studio bug. The most common root causes are: the form class lives in a different namespace or file than you expect; the class is not declared as a managed ref type; the header in your project is not the one the compiler is seeing; or you have a circular-include problem. Below are practical checks and patterns that solve 9 times out of 10.

Checklist to diagnose

  • Make sure the project is compiled as C++/CLI (CLR enabled).
  • Verify the form declaration is a managed type (for example public ref class Form1) and that you reference the correct namespace or fully qualify the type.
  • Confirm the exact header file is part of the project and the include path is correct (watch for duplicate filenames in different folders).
  • Pay attention to case sensitivity: Form1 is not form1.
  • If you get precompiled-header errors, ensure stdafx.h (or your PCH) is included first in each .cpp.

Avoid circular includes (recommended pattern)
Forward-declare the other managed form in the consumer header, e.g. ref class Form1;, store a handle like Form1^ owner; in Form2 and include Form1.h only in Form2.cpp. Keep the public surface on Form1 minimal (public methods/properties for what Form2 needs) so headers remain lightweight.

Alternatives and quick tips
You can also use the form Owner property, Application::OpenForms, or a small public API on Form1 instead of tight coupling. When asking for help (as requested), paste the exact compiler error and the declarations (headers) involved — the error text pinpoints whether it is a missing include, wrong namespace, or a managed/native type mismatch. As noted, handling circular references carefully is key.

Recommended Answers

All 3 Replies

Please post your code. Don't forget to push the code button before you paste each file.

Modify the constructor of form2 to take a form1 object. Assign that to a private form object in your form2. Operate on it like you would any other object.

Caveat: I've been trying to get a sample together and have been having some problems with circular references. It's fairly trivial in C# since there are no headers.

Take a look at http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/097d7025-7c67-444d-909a-a91d05eb298d/ which is one of the clearest explanations I've seen as to how to do this. Also note they give some alternatives too!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.