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.

Text is probably declared private in Form1. In Form1 write a public get and put functions to expose Text to the public.

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?

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?

I just tried it and had no compile errors. Do you have General.h included at the beginning of Form1.h? (recursive includes)

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.

Just as I thought -- recursive includes. Form1.h includes general.h and vice versa.

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.

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?

delete line 4 from general.h. form1.h isn't needed in general.h

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!";

I did, but same errors!

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.

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>

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);
             }
commented: Nice job wrangling this thread into submission :) +14
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.