Weird button click event for button with same EventHandler
-
I have 2 buttons on my form and they're calling the same function when clicked. When button 1 is clicked, the event handler is called. I then debug and read the sender's name in the event handler and see that button 2 was the one being clicked.(same applies when pressing button 2, button 1 becomes the sender's name) This situation happens randomly without pattern, and quite frequently 8/10 times when I click the button. I have no idea what's the reason to this! MyForm.h
#include "Common_Groupbox.h"
.
.
.
private: System::Windows::Forms::Button^ Button1;
private: System::Windows::Forms::Button^ Button2;private: Common_Groupbox^ groupbox1;
private: Common_Groupbox^ groupbox2;
.
.
.
private: System::Void MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e) {groupbox1 = gcnew Common\_Groupbox(Button1); groupbox2 = gcnew Common\_Groupbox(Button2);
}
Common_Groupbox.h
private: System::Windows::Forms::Button^ _myButton;
public:
Common_Groupbox( System::Windows::Forms::Button^ myButton) {\_myButton = myButton; \_myButton->Click += gcnew System::EventHandler(this, &Common\_Groupbox::myButton\_Clicked); } public: System::Void myButton\_Clicked(System::Object^ sender, System::EventArgs^ e) { // do something }