问题描述:

I'm trying to build my own Simple Calculator.

I have this method with 10 references (from 0-10)

private void button_Click_1(object sender, EventArgs e)

{

Button b = (Button)sender;

tb.Text += b.ToString();

}`

Everything is working but the text sent to the TextBox come with extra things that I don't need.

I just want to show the number that I clicked on the calculator. I want to hide the "System.windows.forms.button, Text:" and show only the 7(in this case)

网友答案:

Use the Button.Text property of Button instead of ToString()

 tb.Text += b.Text;
网友答案:

As Adil points out just use the Text property.

A further improvement can be made to your code base by instead of using the generated Event handler, create a single reusable event handler

instead of

private void button_Click_1(object sender, EventArgs e)
{
    Button b = (Button)sender;
    tb.Text += b.Text;
}
private void button_Click_2(object sender, EventArgs e)
{
    Button b = (Button)sender;
    tb.Text += b.Text;
}
private void button_Click_3(object sender, EventArgs e)
{
    ...

You could instead just have the single event handler. Remember to update your pointers on your buttons to point to this event handler.

private void numericButton_Click(object sender, EventArgs e)
{
    Button b = (Button)sender;
    tb.Text += b.Text;
}`
相关阅读:
Top