How can I check if btn1 (a specific button) or any other button has been clicked in another _Click event?

private void btnMPlus_Click(object sender, EventArgs e)
        {
            memory += double.Parse(lblInvoer.Text);
            // -- Do the coding here.. --
        }

If any button or a specific one is clicked, clear lblInvoer..

1 answer

  • answered 2018-11-07 23:13 Olivier Jacot-Descombes

    You can attach the same event handler to several buttons. It is also possible to attach more than one event handler to the same button

    button1.Click += CommonButtonClickHandler;
    button2.Click += CommonButtonClickHandler;
    button3.Click += CommonButtonClickHandler;
    button3.Click += button3_Click; 
    

    Then

    private void CommonButtonClickHandler(object sender, EventArgs e)
    {
        // Do stuff common to all buttons here.
    }
    
    private void button3_Click(object sender, EventArgs e)
    {
        // Do stuff specific to button3 here.
    }
    

    Note that you can also assign the event handlers in the properties window (only one at a time). Select them from the drop down.

    If you want to know if another button has been clicked before, set a flag (a Boolean variable).