How to add or subtract a digit in the Textbox via a button?

Asked

Viewed 1,590 times

5

I want to add to a number in a TextBox generating a digit when the user clicks the " + " button and subtracts when he clicks the " - ". For example: the random number was 2. If I click on +, 3 appears. If I click on -, 1 appears.

From the looks of it, you can’t do addition and subtraction operations with TextBoxes, as in this code here, which was presented the CS00019 error:

private void btn_plus_Click(object sender, EventArgs e)
{ 
resultado.Text = resultado + 1;
}

Where the resultado.Text is the number converted into string and resultado the name of my TextBox.

How could I make it work?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

4

Use the TryParse() if you do not want to generate an error if the value is invalid. You can treat as you wish if it is invalid. In this case I ignored the sum.

private void btn_plus_Click(object sender, EventArgs e) {
    int numero;
    if (int.TryParse(resultado.Text, out numero)) {
        resultado.Text = (numero + 1).ToString();
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference. I had to adapt since I’m not in a Winforms application.

Just use the Parse() (difference) whenever he manages an exception if he can’t parse the number, which is not common to want this. It is better to know that it was wrong and treat right there without the cost of the exception, even more that most programmers treat exceptions wrongly.

  • A question... C# does not error there, the variable numero was declared, but received no value..?

  • @Kaduamaral I had seen a mistake in his too and at the end in the rush what I was going to fix I did not fix in mine, now it’s ok. Not because the TryParse will play a value for it. There is even a possibility of being able to declare so: Int32.TryParse(resultado.Text, out int numero) saving a line. Of course he only plays if he can give the parse, but if you cannot, you will not use the variable. And at the bottom it has a value of 0.

  • Ahh understood. The exit of parse goes to the variable numero, right? I’m just speculating because I’m also studying C# and I didn’t know the method TryParse... :D

  • @Kaduamaral he’s better at 99% of the time. I just forgot to say up there that this syntax of declaring the variable within the method call is something that almost entered C# 6 and has a chance to enter C# 7.

  • What is the mistake you had seen in mine? It is necessary to convert value to string to assign to Textbox?

  • @Kaduamaral.

Show 1 more comment

1

C# is not like PHP that performs operations with what lies ahead, it needs to work with certain types, and as you yourself expressed the value of the Textbox field is text (or string). To work with mathematical operations you first need to convert the value to some numeric type like Int, Float and etc...

Then your code would be...:

Para Somar:

resultado.Text = (int.Parse(resultado.Text) + 1).ToString();

Or to Subtract:

resultado.Text = (int.Parse(resultado.Text) - 1).ToString();

1

private void btn_plus_Click(object sender, EventArgs e)
{ 
    int boxI = resultado.Text        
    resultado.Text = boxI + 1;
}

I haven’t had time to test it, but it should work.

  • Error in the fourth line, convert the boxI to string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.