I created this custom test class:
#include "Form1.h"
class Demo
{
public:
void sayHello()
{
System::Windows::Forms::Form1->Text = "Hello Form!"; // does not work
Form1->Text = "Hello Form!"; // does not work
Form1.Text = "Hello Form!"; // does not work
}
};
I essentially get this compiler error:
c:\users\pieter kubben\documents\visual studio 2010\projects\testclassref\testclassref\Demo.h(8): error C2065: 'Form1' : undeclared identifier
c:\users\pieter kubben\documents\visual studio 2010\projects\testclassref\testclassref\Demo.h(8): error C2227: left of '->Text' must point to class/struct/union/generic type
So I guess it does not see Form1. The other way around, so have the sayHello() function called when clicking the button in Form1, is no problem.
I noticed that my main() function (auto-generated by the IDE) contains this line:
Application::Run(gcnew Form1());
So it looks to me that Form1 has not yet been created when the compiler starts, although Form1.h does exist (of course).
How can I access Form1 elements from my custom class? E.g. change Form1.Text?
Create a global variable to hold your form object and alter the IDE generated to show the form
like this
Now frm is the referecne fo the form1 class and you can access it to access the Form1 class members. If you want to access frm object outside of the Main function then declare a global variable and access it.