Passing data between forms C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qtlib@o2.pl

    Passing data between forms C++

    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

  • Tamas Demjen

    #2
    Re: Passing data between forms C++

    qtlib@o2.pl wrote:
    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(S ystem::Object^ sender,
    System::EventAr gs^ e);

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

    System::Void Form2::button1_ Click(System::O bject^ sender,
    System::EventAr gs^ e)
    {
    form1->label1->Text = "whatever";
    }

    Tom

    Comment

    • David Wilkinson

      #3
      Re: Passing data between forms C++

      Tamas Demjen wrote:
      qtlib@o2.pl wrote:
      >
      >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(S ystem::Object^ sender,
      System::EventAr gs^ e);
      >
      // into Form2.cpp:
      #include "Form1.h"
      >
      System::Void Form2::button1_ Click(System::O bject^ sender,
      System::EventAr gs^ e)
      {
      form1->label1->Text = "whatever";
      }
      >
      Tom
      And people wonder why C++/CLI is not catching on as a first class .net
      language...

      David Wilkinson

      Comment

      Working...