Multiple control items share same method

This article shares that how to make multiple control items share the same method.

In Calutor sample, after clicking the number button, need bring the number on the button into the TextBox above.

So we can write the code like this.
 

private void button1_Click(object sender, EventArgs e)
{
        txtInput.Text = button1.Text;
}

But we have a lot of button to deal ,it would be too troublesome if you deal with to write one by one.
 

At this time, we can switch to event tab from properties windows, and we can see that the Click event is bound to the button1_Click method.
 

we changed the method name to number_Click in the properties windows, and the program page also changed to number_Click.
After adjustment as follows.
 

private void number_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    txtInput.Text = btn.Text;
}

// line 1: changed to number_Click.
// line 3: The general object sender is forcibly converted to the Button category and named btn.
// (Warning)can only be converted to Button, otherwise an error will be reported.

Finally, just set all the number button to the number_Click method, and all the buttons are set!!