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)
        {
            addDigit('7');
        }

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

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

This is a recent thread on how you can do it: http://www.daniweb.com/forums/thread351266.html
Another tip: Put your buttons in an Array or Collection so you can easiky place them on the form via a loop. In this case I should not do it via the designer.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.