Hi there,

I am new in C++/CLR. I am trying to access to Form1 property from a header file, but I can't. I have included this header file in Form1.h. Here is the source of the header file:

// General.h

#ifndef GENERAL_H
#define GENERAL_H
#include "Form1.h"
void ChangeFormText()
{
    Form1^ form1 =gcnew Form1();
        form1->Text="Hello!";
}
#endif 

I REALLY appreciate you help.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

thank you for your quick reply. with the TextBox declared public, it still shows same errors:

1>c:\users\mmd\documents\visual studio 2010\projects\888\888\mynewheader.h(6): error C2065: 'Form1' : undeclared identifier
1>c:\users\mmd\documents\visual studio 2010\projects\888\888\mynewheader.h(6): error C2065: 'form1' : undeclared identifier
1>c:\users\mmd\documents\visual studio 2010\projects\888\888\mynewheader.h(6): error C2061: syntax error : identifier 'Form1'
1>c:\users\mmd\documents\visual studio 2010\projects\888\888\mynewheader.h(7): error C2065: 'form1' : undeclared identifier
1>c:\users\mmd\documents\visual studio 2010\projects\888\888\mynewheader.h(7): error C2227: left of '->Text' must point to class/struct/union/generic type
1> type is ''unknown-type''

any idea?

Edited 5 Years Ago by msrox: n/a

It is not finding the Form1 object in the Form1.h header file. Is that the name of the object?

Yes, but it seems that it can not find Form1 inside General.h . doesn't it?

Well you are performing these operation from within General.h aren't you? That would mean that the compiler cannot find the Form1 object inside Form1.h

Yes. That's what I need to have access to Form1 from functions inside General.h.

Any idea please?

It has errors in my case. This is the Form1.h source:

#pragma once
#include "General.h"
namespace My666 {
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1() {
			InitializeComponent();
		} 
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(70, 74);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 273);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 ChangeFormText();
			 }
	};
}

please let me know if you what is wrong.

So what do I need to do now? How is your code written that has no compile error please?

A couple solutions:

1. Delete general.h from Form1.h and just add function prototype for the function that's in general.h.

// Form1.h
 #pragma once
extern void ChangeFormText(); // <<<<<<<<<<<<<
namespace My666 {
	using namespace System;
	using namespace System::ComponentModel;

2. Put the implemnentation code for the function in general.h in general.cpp then only have the function prototype in general.h.

Edited 5 Years Ago by Ancient Dragon: n/a

I tried the second solution but still same errors. These are sources for General.h and General.cpp:

// General.h
#ifndef GENERAL_H
#define GENERAL_H
#include "Form1.h"
void ChangeFormText();
#endif
//General.cpp
#include "StdAfx.h"
#include "Form1.h"
void ChangeFormText()
{
	Form1^ form1 =gcnew Form1();
	form1->Text="Hello!";
}

These are the errors:

1>General.cpp(6): error C2065: 'Form1' : undeclared identifier
1>General.cpp(6): error C2065: 'form1' : undeclared identifier
1>General.cpp(6): error C2061: syntax error : identifier 'Form1'
1>General.cpp(7): error C2065: 'form1' : undeclared identifier
1>General.cpp(7): error C2227: left of '->Text' must point to class/struct/union/generic type
1>          type is ''unknown-type''

what do u think?

I tried that before, with no luck! still same errors!

now what else can I try?

//General.cpp
#include "StdAfx.h"
#include "general.h" // <<<<<<<<<<<<< add this line
#include "Form1.h"
void ChangeFormText()
{
	Form1^ form1 =gcnew Form1();
	form1->Text="Hello!";
}

Exact same errors:

General.cpp(9): error C2065: 'Form1' : undeclared identifier
1>General.cpp(9): error C2065: 'form1' : undeclared identifier
1>General.cpp(9): error C2061: syntax error : identifier 'Form1'
1>General.cpp(10): error C2065: 'form1' : undeclared identifier
1>General.cpp(10): error C2227: left of '->Text' must point to class/struct/union/generic type
1>          type is ''unknown-type''

what is wrong now pls?

When you look at your Form1.h and General.h header files you do not have any references to either .h file right? The only reference to Form1.h and General.h that you should have is in the General.cpp file

There was a reference to General.h in Form1.h and I removed it, but errors are still exist. The question is that how Form1.h know that there is a General.h exists.
Anyway these are the whole codes:

#pragma once

namespace My666 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1() {
			InitializeComponent();
			
		} 

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;

	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(70, 74);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 273);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 ChangeFormText();
			 }
	};
}
// General.h

#ifndef GENERAL_H
#define GENERAL_H

void ChangeFormText();
#endif
//General.cpp

#include "StdAfx.h"
#include "General.h"
#include "Form1.h"

void ChangeFormText()
{
	Form1^ form1 =gcnew Form1();
	form1->Text="Hello!";
}

I appreciate your reply.

The problem now is that in general.cpp you have to identify the namespace that Form1 resides in.

#include "stdafx.h"
#include "Form1.h"


void ChangeFormText()
{
Form1::Form1^ form1 =gcnew Form1::Form1;
form1->textBox1->Text="Hello!";
}

I added the namespace to General.cpp :

//General.cpp

#include "StdAfx.h"
#include "General.h"
#include "Form1.h"

namespace My666
{
void ChangeFormText()
{
	Form1^ form1 =gcnew Form1();
	form1->Text="Hello!";
}
}

I also tried this:

//General.cpp

#include "StdAfx.h"
#include "General.h"
#include "Form1.h"

using namespace My666;

void ChangeFormText()
{
	Form1^ form1 =gcnew Form1();
	form1->Text="Hello!";

}

Now this is the error it shows:

error C3861: 'ChangeFormText': identifier not found

any help?

Try using the exact code written by AD

Form1::Form1^ form1 =gcnew Form1::Form1;
form1->textBox1->Text="Hello!";

Edited 5 Years Ago by abhimanipal: n/a

Attached are all the files I used. There must be something wrong with the way you are trying to do it. Compare the files I have with those you have.

Attachments
// Form1.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace Form1;

[STAThreadAttribute]
int main(array<System::String ^> ^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::Form1());
	return 0;
}
#pragma once
    extern void ChangeFormText();

namespace Form1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
    private: System::Windows::Forms::MenuStrip^  menuStrip1;
    protected: 
    private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  openToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  newToolStripMenuItem;
    private: System::Windows::Forms::ToolStripMenuItem^  closeToolStripMenuItem;
    public: System::Windows::Forms::TextBox^  textBox1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
            this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
            this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->openToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->newToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->closeToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->menuStrip1->SuspendLayout();
            this->SuspendLayout();
            // 
            // menuStrip1
            // 
            this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->fileToolStripMenuItem});
            this->menuStrip1->Location = System::Drawing::Point(0, 0);
            this->menuStrip1->Name = L"menuStrip1";
            this->menuStrip1->Size = System::Drawing::Size(376, 28);
            this->menuStrip1->TabIndex = 0;
            this->menuStrip1->Text = L"menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->openToolStripMenuItem, 
                this->newToolStripMenuItem, this->closeToolStripMenuItem});
            this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
            this->fileToolStripMenuItem->Size = System::Drawing::Size(44, 24);
            this->fileToolStripMenuItem->Text = L"File";
            // 
            // openToolStripMenuItem
            // 
            this->openToolStripMenuItem->Name = L"openToolStripMenuItem";
            this->openToolStripMenuItem->Size = System::Drawing::Size(114, 24);
            this->openToolStripMenuItem->Text = L"Open";
            // 
            // newToolStripMenuItem
            // 
            this->newToolStripMenuItem->Name = L"newToolStripMenuItem";
            this->newToolStripMenuItem->Size = System::Drawing::Size(114, 24);
            this->newToolStripMenuItem->Text = L"New";
            // 
            // closeToolStripMenuItem
            // 
            this->closeToolStripMenuItem->Name = L"closeToolStripMenuItem";
            this->closeToolStripMenuItem->Size = System::Drawing::Size(114, 24);
            this->closeToolStripMenuItem->Text = L"Close";
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(107, 132);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(100, 22);
            this->textBox1->TabIndex = 1;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(376, 344);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->menuStrip1);
            this->MainMenuStrip = this->menuStrip1;
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->menuStrip1->ResumeLayout(false);
            this->menuStrip1->PerformLayout();
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 ChangeFormText();
			 }
	};

}
#include "stdafx.h"
#include "Form1.h"


void ChangeFormText()
{
Form1::Form1^ form1 =gcnew Form1::Form1;
form1->textBox1->Text="Hello!";
}
#ifndef GENERAL_H
#define GENERAL_H
#include "Form1.h"
void ChangeFormText();
#endif

I created a new project with appropriate files named as Form1 and General and copy/paste your codes. It has no compile error now, but it doesn't work. It does not change the textBox's text.

I guess we have to send the form1 to the ChangeFormText() by defining it like ChangeFormText(Form1^ frm) and using it like ChangeFormText(this) in the button click event. What do you think?

It doesn't work because ChangeFormText() allocates a new object of type Form1 and changes it text. That object is immediately destroyed when ChageFormText() returns to its caller.

You will have to put ChangeFormText() in Form1 namespace and add Form1^ parameter. Also the project I gave you does not contain a button.

// general.h
#ifndef GENERAL_H
#define GENERAL_H
#include "Form1.h"
namespace Form1 {
void ChangeFormText(Form1^ form1);
};
#endif
// general.cpp
#include "stdafx.h"
#include "Form1.h"

namespace Form1 {

void ChangeFormText(Form1^ form1)
{
form1->textBox1->Text="Hello!";
}
};
// Form1.h (first few lines)
#pragma once

namespace Form1 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

    ref class Form1;
    extern void ChangeFormText(Form1^ fm);

	/// <summary>
	/// Summary for Form1
	/// </summary>

Edited 5 Years Ago by Ancient Dragon: n/a

It should work now. Thank you for all your helps. :)

Forgot to post this in Form1.h

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 ChangeFormText(this);
             }
Comments
Nice job wrangling this thread into submission :)
This article has been dead for over six months. Start a new discussion instead.