title: Windows Forms Application: accessing elements in other forms visual studio 2012 c++
body: <p>Accessing (adding) an element of Form2 to Form1 works just fine with c++ vs2010.
  If I try the same thing from scratch in visual studio express 2012, I keep getting an error saying Form2 is undeclared identifier. Any ideas what is wrong?</p>
  <pre><code>    #include &quot;stdafx.h&quot;
      #include &quot;Form2.h&quot;
      ....
      
      Form2^ frm = gcnew Form2;
      this-&gt;Controls-&gt;Add(frm-&gt;panel1);
  </code></pre>
  <p>error C2065: 'Form2' : undeclared identifier</p>
  <p>Code Form1:</p>
  <h1>include &quot;stdafx.h&quot;</h1>
  <h1>include &quot;Form2.h&quot;</h1>
  <pre><code>    #pragma once
  
      namespace WindowsFormsApplication{
  
          using namespace System;
          using namespace System::ComponentModel;
          using namespace System::Collections;
          using namespace System::Windows::Forms;
          using namespace System::Data;
          using namespace System::Drawing;
  
  
  
  
  
          /// &lt;summary&gt;
          /// Summary for Form1
          /// &lt;/summary&gt;
          public ref class Form1 : public System::Windows::Forms::Form
          {
          public:
              Form1(void)
              {
                  InitializeComponent();
                  //
                  //TODO: Add the constructor code here
                  //
              }
  
          protected:
              /// &lt;summary&gt;
              /// Clean up any resources being used.
              /// &lt;/summary&gt;
              ~Form1()
              {
                  if (components)
                  {
                      delete components;
                  }
              }
          private: System::Windows::Forms::Button^  button1;
          protected: 
  
          private:
              /// &lt;summary&gt;
              /// Required designer variable.
              /// &lt;/summary&gt;
              System::ComponentModel::Container ^components;
  
      #pragma region Windows Form Designer generated code
              /// &lt;summary&gt;
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// &lt;/summary&gt;
              void InitializeComponent(void)
              {
                  this-&gt;button1 = (gcnew System::Windows::Forms::Button());
                  this-&gt;SuspendLayout();
                  // 
                  // button1
                  // 
                  this-&gt;button1-&gt;Location = System::Drawing::Point(31, 32);
                  this-&gt;button1-&gt;Name = L&quot;button1&quot;;
                  this-&gt;button1-&gt;Size = System::Drawing::Size(75, 27);
                  this-&gt;button1-&gt;TabIndex = 0;
                  this-&gt;button1-&gt;Text = L&quot;button1&quot;;
                  this-&gt;button1-&gt;UseVisualStyleBackColor = true;
                  this-&gt;button1-&gt;Click += gcnew System::EventHandler(this,   &amp;Form1::button1_Click);
                  // 
                  // Form1
                  // 
                  this-&gt;AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                  this-&gt;AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                  this-&gt;ClientSize = System::Drawing::Size(284, 262);
                  this-&gt;Controls-&gt;Add(this-&gt;button1);
                  this-&gt;Name = L&quot;Form1&quot;;
                  this-&gt;Text = L&quot;Form1&quot;;
                  this-&gt;ResumeLayout(false);
  
              }
      #pragma endregion
          private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                       Form2^ frm = gcnew Form2;
              this-&gt;Controls-&gt;Add(frm-&gt;panel1);
               
                   }
          };
      }
  </code></pre>
  <p>Code Form2:</p>
  <pre><code>    #pragma once
  
      namespace Windows_Forms_Application{
  
          using namespace System;
          using namespace System::ComponentModel;
          using namespace System::Collections;
          using namespace System::Windows::Forms;
          using namespace System::Data;
          using namespace System::Drawing;
  
          /// &lt;summary&gt;
          /// Summary for Form2
          /// &lt;/summary&gt;
          public ref class Form2 : public System::Windows::Forms::Form
          {
          public:
              Form2(void)
              {
                  InitializeComponent();
                  //
                  //TODO: Add the constructor code here
                  //
              }
  
          protected:
              /// &lt;summary&gt;
              /// Clean up any resources being used.
              /// &lt;/summary&gt;
              ~Form2()
              {
                  if (components)
                  {
                      delete components;
                  }
              }
          public: System::Windows::Forms::Panel^  panel1;
          protected: 
  
          private:
              /// &lt;summary&gt;
              /// Required designer variable.
              /// &lt;/summary&gt;
              System::ComponentModel::Container ^components;
  
      #pragma region Windows Form Designer generated code
              /// &lt;summary&gt;
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// &lt;/summary&gt;
              void InitializeComponent(void)
              {
                  this-&gt;panel1 = (gcnew System::Windows::Forms::Panel());
                  this-&gt;SuspendLayout();
                  // 
                  // panel1
                  // 
                  this-&gt;panel1-&gt;BackColor =        System::Drawing::SystemColors::ActiveCaptionText;
                  this-&gt;panel1-&gt;Location = System::Drawing::Point(106, 85);
                  this-&gt;panel1-&gt;Name = L&quot;panel1&quot;;
                  this-&gt;panel1-&gt;Size = System::Drawing::Size(132, 118);
                  this-&gt;panel1-&gt;TabIndex = 0;
                  // 
                  // Form2
                  // 
                  this-&gt;AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                  this-&gt;AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                  this-&gt;ClientSize = System::Drawing::Size(284, 262);
                  this-&gt;Controls-&gt;Add(this-&gt;panel1);
                  this-&gt;Name = L&quot;Form2&quot;;
                  this-&gt;Text = L&quot;Form2&quot;;
                  this-&gt;ResumeLayout(false);
  
              }
      #pragma endregion
          };
      }
  </code></pre>
  <p>Code cpp:</p>
  <pre><code>    // Windows Forms Application.cpp : main project file.
  
      #include &quot;stdafx.h&quot;
      #include &quot;Form2.h&quot;
      #include &quot;Form1.h&quot;
  
      using namespace WindowsFormsApplication;
  
      [STAThreadAttribute]
      int main(array&lt;System::String ^&gt; ^args)
      {
          // Enabling Windows XP visual effects before any controls are created
          Application::EnableVisualStyles();
          Application::SetCompatibleTextRenderingDefault(false); 
  
          // Create the main window and run it
          Application::Run(gcnew Form1());
          return 0;
      }
  </code></pre>