Hello!

There are two windows: Form1 (parent), Form2 (child); It is impossible to get access to the variable \ function of the parent window. Here is the code:

//Main: #include "stdafx.h" #include <iostream> using namespace std; #include "Form2.h" #include "Form3.h" #include "Form1.h" using namespace Server_WFA; [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()); return 0; } //Form1: #pragma once namespace Server_WFA { 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: void opener(){} Form1() { InitializeComponent(); opener(); form2 = gcnew Form2(this); } Form2^ form2; .... //Form2: #pragma once #include <iostream> using namespace std; namespace Server_WFA { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Runtime::InteropServices; /// <summary> /// Summary for Form2 /// </summary> ref class Form1;//Дообьявляем! public ref class Form2 : public System::Windows::Forms::Form { private: Form1^ owner; public: Form2(Form1^ own) { InitializeComponent(); owner = own; this->Owner->Name=L"";//opener(); this->Text=owner->label1->Text; owner->opener(); // ничего не работает!!! } 

Some bugs:

error C2027: use of undefined type 'Server_WFA :: Form1'
error C2227: left of '-> opener' must point to class / struct / union / generic type

How to access the opener function? located in form1 ?

  • Can anyone have the source code with two windows that can refer to each other? - Alerr
  • Maybe have. VladD gave a good answer, what's the problem? - nitrocaster
  • The problem is that it does not work ... Here is the code: Form2.cpp #include "StdAfx.h" #include "Form1.h" #include "Form2.h" namespace Server_WFA {// I use the space of the first form using namespace System; ... void Form2 :: InitializeComponent (void) {.. Errors: 1> Server_WFA.obj: error LNK2020: unresolved token (06000001) Server_WFA.Form2 ::. ctor 1> Form2.obj: error LNK2020: unresolved token (06000008 ) Server_WFA.Form2 ::. Ctor 1> Form1.obj: error LNK2020: unresolved token (06000008) Server_WFA.Form2 ::. Ctor 1> ... \ Server_WFA is a copy of \ Debug \ Server_WFA.exe: fatal error LNK1120: 3 unresolved externals - Alerr
  • What is wrong with these windows? Where can I read about it? I was looking for something like that, I did not find it, they are taught to create a window, but they don’t learn how to contact the parent ... - Alerr
  • Everything, everything works !!! Thanks VladD !!! - Alerr

1 answer 1

First, if you write on WinForms, go to C #, there are no problems with manual visibility control.

Secondly, C ++ / CLI is a language based on C ++ and inheriting its unusual features. In your case, forward ads

 ref class Form1; 

not enough. You need to break the classes into declarations (which go to .h ) and implementation (which go to .cpp ), and connect the declarations where appropriate, as usual in C ++.

Example:

 // Form1.h ref class Form2; // достаточно forward-декларации public ref class Form1 : public System::Windows::Forms::Form { public: void opener(); Form1(); private: Form2^ form2; .... }; // Form1.cpp // forward-декларации недостаточно, нужен доступ к функциям #include "Form2.h" ... void Form1::opener() { // ... } Form1::Form1() { InitializeComponent(); opener(); form2 = gcnew Form2(this); } 
  • But how to deal with Wed? Where to connect them? K cfr with main? As far as I understand you: 1) It is necessary to keep class constructions in .h, and to write the corresponding functions in cpp, including the functions of events. Right? Why so strange? - Alerr
  • @Alerr: .cpp is not necessary to connect anywhere, just let it be in the project. Regarding this separation - well, it once had its own logic, and now it exists for backward compatibility. Think of it as a strange C ++ language. - VladD
  • I did, added functions to the Wed, got errors like error C2653: 'Form1': not a class or namespace name 1> Form1.cpp (11): error C2653: 'MessageBox': not a class or namespace name. It seems there is no access to these elements ... - Alerr
  • @Alerr: haven't you forgotten #include in the beginning .cpp? namespace added? - VladD
  • Corrected: #include "StdAfx.h" #include "Form2.h" namespace Server_WFA {using namespace System; using namespace System :: ComponentModel; using namespace System :: Collections; using namespace System :: Windows :: Forms; using namespace System :: Data; using namespace System :: Drawing; void Form1 :: opener () {} .... A lot of errors, for example, it swears at the constructor ... The destructor left in the class ... error C2653: 'Form1': is not a class or namespace name - Alerr