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. Weird button click event for button with same EventHandler

Weird button click event for button with same EventHandler

Scheduled Pinned Locked Moved Managed C++/CLI
debuggingregex
1 Posts 1 Posters 4 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.
  • Y Offline
    Y Offline
    yehiga1467
    wrote on last edited by
    #1

    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 
    
    }
    
    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