I need to make calculator with buttons for each digit and so on. My question is, if there is any way to make one function that will handle with all events for all digits?

Now I have:

private void addDigit(char i)
            // some calculation with 'i'
        private void btn7_Click(object sender, EventArgs e)

but I have 10 methods. Is there any other option? To make one method for all with digit as an argument?

Edited by iustitia: n/a

7 Years
Discussion Span
Last Post by shootermac

Great. That is what I was looking for. Thanks.
Nice tip - but I don't think it takes so much time to place buttons via designer. It's quite simple. But maybe for bigger amount of buttons it makes more difference.


As you can see from the code in the thread link a lot of initialisation code for every button is the same. With an array of buttons, you only need one block of that code and let for instance change the buttons location through the loop variable.
If you have 2 to 3 buttons it is manageable with the designer, but a calculator typically has 10 or more buttons. I think it is better to use an array i that situation.
For you, the developer to decide. Success!


the sender argument will tell you which button you pressed. You just need to cast it to a button type.

This is the correct answer, in the gui you assign the same method to each event.

Or alternatively just use different click events and call the method from there.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.