Get the button click event from another form
-
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?
-
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?
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 :)
-
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 :)
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
-
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
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
-
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
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?
-
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?
it was just the name i used...so no reason for that
-
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?