Catching multiple buttons in one function

I'm creating a windows form application with an awfull lot of buttons. 81 to be precise.
I have set an event on all these buttons to one function: changeButtonToTextbox(object sender, MouseEventArgs e)

This works fine, but the program needs to know which button really is clicked.
I can do this with a simple if(sender == button1). But I don't feel like making a switch statement with 81 possibilities.
It wouldn't be efficient too.

Suppose I want to change one property of a button; visibility. How would I do this
How can I determine which button is clicked and respond on that by making the button invisible

TiA,

MaximusBrood



Answer this question

Catching multiple buttons in one function

  • Amit Nalla

    Yup, just write one line of code to do this

    private void button1_Click(object sender, EventArgs e)
    {
    ((Button)sender).Visible = false;
    }

  • KentDy

    Hi,

    Button b = (Button) sender;
    b.visible = false;

    please take care of possible typo mistakes I did not test this in visual studio.

    The idea is that you get an object reference to the pressed button via sender. So you actually can cast this into a button object and use it's properties and methods, without actually knowing the name of the pressed button.



  • GuruprasadTG

    Allright. Thanks both of you.
    I've tested and it works perfectly

    MaximusBrood

  • Catching multiple buttons in one function