Passing data between forms C++

Q

qtlib

How to pass data between two forms?
I find in the Internet, but not found immplementation problem in Visual
Studio 2005 and C++ language.

Example:
I have a 2 forms: Form1 and Form2.

On a Form1 place label1 and button
First I declare: #include "Form2.h"

Next, I wrote Event button (show Form2):

Form2 ^frm2 = gcnew Form2();
frm2->Owner = this;
frm2->ShowDialog();

On a Form2 place textbox and button.
When I click button, change label1 on a Form1.

How to write this Event ?
(if I declare in source Form2: #include "Form1.h", compile with error!
)

In C# it's simple, but in C++ it's difficult.

Please help me
 
T

Tamas Demjen

On a Form2 place textbox and button.
When I click button, change label1 on a Form1.

The problem is that Form1.h includes Form2.h, and Form2.h includes
Form1.h, which is not possible. You should move the implementation of
the event handlers to the .cpp file. Form2.cpp should then include
Form1.h. I know that the designer puts the implementation in the header
file by default, but it's simply not possible to do what you want
without manually moving your implementation to where it belongs.

That is:

// into Form2.h:
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e);

// into Form2.cpp:
#include "Form1.h"

System::Void Form2::button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
form1->label1->Text = "whatever";
}

Tom
 
D

David Wilkinson

Tamas said:
The problem is that Form1.h includes Form2.h, and Form2.h includes
Form1.h, which is not possible. You should move the implementation of
the event handlers to the .cpp file. Form2.cpp should then include
Form1.h. I know that the designer puts the implementation in the header
file by default, but it's simply not possible to do what you want
without manually moving your implementation to where it belongs.

That is:

// into Form2.h:
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e);

// into Form2.cpp:
#include "Form1.h"

System::Void Form2::button1_Click(System::Object^ sender,
System::EventArgs^ e)
{
form1->label1->Text = "whatever";
}

Tom

And people wonder why C++/CLI is not catching on as a first class .net
language...

David Wilkinson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top