Godace
Godace

Reputation: 11

how to fix this EventArgs c#

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.

Image

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)

Upvotes: 1

Views: 115

Answers (2)

Lee Campbell
Lee Campbell

Reputation: 10783

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;
}`

Upvotes: 1

Adil
Adil

Reputation: 148140

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

 tb.Text += b.Text;

Upvotes: 4

Related Questions