Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Get the button click event from another form

Get the button click event from another form

Scheduled Pinned Locked Moved Managed C++/CLI
tutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rikterveen
    wrote on last edited by
    #1

    I'm struggling for a couple of days now. i have two forms, The main form and some popup form with textboxes. I open the popupform from the mainform then fill in some information on the popupform and send it with a button. When that button is clicked the mainform has to detect it and grab the data from the popupform before closing it. but so far i'm not able to get that click event from the popupform i tried the following which will compile but does nothing:

    rowpopup^ popup = gcnew rowpopup();
    popup->button_savetoform->Click += gcnew EventHandler(this, &Main::add_new_item);

    I got some advice to work with delegates and after a lot of reading and googling i still couldn't find how to use it in this situation. Does anyone have an idea?

    A V 2 Replies Last reply
    0
    • R rikterveen

      I'm struggling for a couple of days now. i have two forms, The main form and some popup form with textboxes. I open the popupform from the mainform then fill in some information on the popupform and send it with a button. When that button is clicked the mainform has to detect it and grab the data from the popupform before closing it. but so far i'm not able to get that click event from the popupform i tried the following which will compile but does nothing:

      rowpopup^ popup = gcnew rowpopup();
      popup->button_savetoform->Click += gcnew EventHandler(this, &Main::add_new_item);

      I got some advice to work with delegates and after a lot of reading and googling i still couldn't find how to use it in this situation. Does anyone have an idea?

      A Offline
      A Offline
      Andreoli Carlo
      wrote on last edited by
      #2

      the code you write is actually....working....so...if you write here or post as attachment the full code i will check what's wrong :)

      R 1 Reply Last reply
      0
      • A Andreoli Carlo

        the code you write is actually....working....so...if you write here or post as attachment the full code i will check what's wrong :)

        R Offline
        R Offline
        rikterveen
        wrote on last edited by
        #3

        OK, i wrote a little test program which does the same.

        #pragma once

        #include "form2.h"

        namespace Test2 {

        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
        
        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
        	Form1(void)
        	{
        		InitializeComponent();
        	}
        
        protected:
        	~Form1()
        	{
        		if (components)
        		{
        			delete components;
        		}
        	}
        private: System::Windows::Forms::Button^  testbutton;
        private: System::Windows::Forms::Label^  testlabel;
        private: System::ComponentModel::Container ^components;
        

        #pragma region Windows Form Designer generated code

        public:
        	void InitializeComponent(void)
        	{
        		this->testbutton = (gcnew System::Windows::Forms::Button());
        		this->testlabel = (gcnew System::Windows::Forms::Label());
        		this->SuspendLayout();
        
        		/\* Testbutton \*/
        		this->testbutton->Location = System::Drawing::Point(12, 326);
        		this->testbutton->Name = L"button\_test";
        		this->testbutton->Size = System::Drawing::Size(75, 23);
        		this->testbutton->TabIndex = 0;
        		this->testbutton->Text = L"Test";
        		this->testbutton->UseVisualStyleBackColor = true;
        		this->testbutton->Click += gcnew System::EventHandler(this, &Form1::testbutton\_click);
        		// 
        		// testlabel
        		// 
        		this->testlabel->AutoSize = true;
        		this->testlabel->Location = System::Drawing::Point(9, 19);
        		this->testlabel->Name = L"label\_test";
        		this->testlabel->Size = System::Drawing::Size(35, 13);
        		this->testlabel->TabIndex = 1;
        		this->testlabel->Text = L"";
        		// 
        		// Form1
        		// 
        		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        		this->ClientSize = System::Drawing::Size(382, 361);
        		this->Controls->Add(this->testlabel);
        		this->Controls->Add(this->testbutton);
        		this->Name = L"Form1";
        		this->Text = L"Form1";
        		this->ResumeLayout(false);
        		this->PerformLayout();
        
        		/\*  Code form the button on other form \*/
        		Form2^ popup = gcnew Form2();
        		popup->button\_send->Click += gcnew System::EventHandler(this, &Form1::raised\_event);
        
        	}
        private:
        

        #pragma endregion

        private: System::Void testbutton\_click(System::Object^  sender, System::Eve
        
        A 1 Reply Last reply
        0
        • R rikterveen

          OK, i wrote a little test program which does the same.

          #pragma once

          #include "form2.h"

          namespace Test2 {

          using namespace System;
          using namespace System::ComponentModel;
          using namespace System::Collections;
          using namespace System::Windows::Forms;
          using namespace System::Data;
          using namespace System::Drawing;
          
          public ref class Form1 : public System::Windows::Forms::Form
          {
          public:
          	Form1(void)
          	{
          		InitializeComponent();
          	}
          
          protected:
          	~Form1()
          	{
          		if (components)
          		{
          			delete components;
          		}
          	}
          private: System::Windows::Forms::Button^  testbutton;
          private: System::Windows::Forms::Label^  testlabel;
          private: System::ComponentModel::Container ^components;
          

          #pragma region Windows Form Designer generated code

          public:
          	void InitializeComponent(void)
          	{
          		this->testbutton = (gcnew System::Windows::Forms::Button());
          		this->testlabel = (gcnew System::Windows::Forms::Label());
          		this->SuspendLayout();
          
          		/\* Testbutton \*/
          		this->testbutton->Location = System::Drawing::Point(12, 326);
          		this->testbutton->Name = L"button\_test";
          		this->testbutton->Size = System::Drawing::Size(75, 23);
          		this->testbutton->TabIndex = 0;
          		this->testbutton->Text = L"Test";
          		this->testbutton->UseVisualStyleBackColor = true;
          		this->testbutton->Click += gcnew System::EventHandler(this, &Form1::testbutton\_click);
          		// 
          		// testlabel
          		// 
          		this->testlabel->AutoSize = true;
          		this->testlabel->Location = System::Drawing::Point(9, 19);
          		this->testlabel->Name = L"label\_test";
          		this->testlabel->Size = System::Drawing::Size(35, 13);
          		this->testlabel->TabIndex = 1;
          		this->testlabel->Text = L"";
          		// 
          		// Form1
          		// 
          		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
          		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
          		this->ClientSize = System::Drawing::Size(382, 361);
          		this->Controls->Add(this->testlabel);
          		this->Controls->Add(this->testbutton);
          		this->Name = L"Form1";
          		this->Text = L"Form1";
          		this->ResumeLayout(false);
          		this->PerformLayout();
          
          		/\*  Code form the button on other form \*/
          		Form2^ popup = gcnew Form2();
          		popup->button\_send->Click += gcnew System::EventHandler(this, &Form1::raised\_event);
          
          	}
          private:
          

          #pragma endregion

          private: System::Void testbutton\_click(System::Object^  sender, System::Eve
          
          A Offline
          A Offline
          Andreoli Carlo
          wrote on last edited by
          #4

          so....here we are: in form 1 substitute:private: System::Void testbutton_click(System::Object^ sender, System::EventArgs^ e) { testlabel->Text = "new form open"; Form2^ popup = gcnew Form2(); popup->ShowDialog(); } private: void raised_event (System::Object^ sender, System::EventArgs^ e) { /* Get the text from the textbox on the other form */ Form2^ popup = gcnew Form2(); testlabel->Text = popup->textbox->Text; } }; // susbstitute with..... private: Form2^ popup; void testbutton_click(System::Object^ sender, System::EventArgs^ e) { testlabel->Text = "new form open"; popup = gcnew Form2(); popup->button_send->Click += gcnew EventHandler(this, &Form1::raised_event); popup->ShowDialog(); } void raised_event (System::Object^ sender, System::EventArgs^ e) { testlabel->Text = popup->textbox->Text; }
          i'm pretty sure you'll understand my changes. Maybe it would be better to use delegates....i'll write you some code for that // form 1///////////////////////////////////////////////////////// #pragma once #include "Form2.h" namespace delete1 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: void testbutton_click(System::Object^ sender, System::EventArgs^ e) { testlabel->Text = "new form open"; Form2^ popup = gcnew Form2(); popup->myPersonalDelegateCl += gcnew Form2::myPersonalDelegate(this,&Form1::raised_from_delegate); popup->ShowDialog(); } void raised_from_delegate (String ^txt) { this->testlabel->Text=txt; } System::Windows::Forms::Button^ testbutton; System::Windows::Forms::Label^ testlabel; System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->testbutton = (gcnew System::Windows::Forms::Button()); this->testlabel = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); /* Testbutton */ this->te

          R 1 Reply Last reply
          0
          • A Andreoli Carlo

            so....here we are: in form 1 substitute:private: System::Void testbutton_click(System::Object^ sender, System::EventArgs^ e) { testlabel->Text = "new form open"; Form2^ popup = gcnew Form2(); popup->ShowDialog(); } private: void raised_event (System::Object^ sender, System::EventArgs^ e) { /* Get the text from the textbox on the other form */ Form2^ popup = gcnew Form2(); testlabel->Text = popup->textbox->Text; } }; // susbstitute with..... private: Form2^ popup; void testbutton_click(System::Object^ sender, System::EventArgs^ e) { testlabel->Text = "new form open"; popup = gcnew Form2(); popup->button_send->Click += gcnew EventHandler(this, &Form1::raised_event); popup->ShowDialog(); } void raised_event (System::Object^ sender, System::EventArgs^ e) { testlabel->Text = popup->textbox->Text; }
            i'm pretty sure you'll understand my changes. Maybe it would be better to use delegates....i'll write you some code for that // form 1///////////////////////////////////////////////////////// #pragma once #include "Form2.h" namespace delete1 { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: void testbutton_click(System::Object^ sender, System::EventArgs^ e) { testlabel->Text = "new form open"; Form2^ popup = gcnew Form2(); popup->myPersonalDelegateCl += gcnew Form2::myPersonalDelegate(this,&Form1::raised_from_delegate); popup->ShowDialog(); } void raised_from_delegate (String ^txt) { this->testlabel->Text=txt; } System::Windows::Forms::Button^ testbutton; System::Windows::Forms::Label^ testlabel; System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->testbutton = (gcnew System::Windows::Forms::Button()); this->testlabel = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); /* Testbutton */ this->te

            R Offline
            R Offline
            rikterveen
            wrote on last edited by
            #5

            The delegate works perfect Thank you :thumbsup: the other code raised the event but didn't get the information from the textbox. so i'll study the delegate. Just one quiestion you changed the namespace of Form1 this results in compiler errors. is there a reason you did this?

            A 1 Reply Last reply
            0
            • R rikterveen

              The delegate works perfect Thank you :thumbsup: the other code raised the event but didn't get the information from the textbox. so i'll study the delegate. Just one quiestion you changed the namespace of Form1 this results in compiler errors. is there a reason you did this?

              A Offline
              A Offline
              Andreoli Carlo
              wrote on last edited by
              #6

              it was just the name i used...so no reason for that

              1 Reply Last reply
              0
              • R rikterveen

                I'm struggling for a couple of days now. i have two forms, The main form and some popup form with textboxes. I open the popupform from the mainform then fill in some information on the popupform and send it with a button. When that button is clicked the mainform has to detect it and grab the data from the popupform before closing it. but so far i'm not able to get that click event from the popupform i tried the following which will compile but does nothing:

                rowpopup^ popup = gcnew rowpopup();
                popup->button_savetoform->Click += gcnew EventHandler(this, &Main::add_new_item);

                I got some advice to work with delegates and after a lot of reading and googling i still couldn't find how to use it in this situation. Does anyone have an idea?

                V Offline
                V Offline
                voo doo12
                wrote on last edited by
                #7

                there is simplest way if you are still enterested

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups