简体   繁体   中英

C# WinForms - Adding two textbox automatically

I'm building this three textbox where if the two textbox are filled the sum will be displayed however, when I input 10 + 10. The result is 1010. Can anbody help me with this?

Here's my code:

public void textBoxTranspo_TextChanged(object sender, EventArgs e) 
{
    if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
        textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text).ToString());
}

public void textBoxDaily_TextChanged(object sender, EventArgs e)
{   
    if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
        textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text).ToString());
}

It is the magic(polymorphism) of the + operator. it will add the values of two operands if the operands are of numeric types(int,long, double) and it will concatenate two operands if they are of type strings or even one string and second integer(as like in your case). Here in your case the .ToString() after convert creates the issues. You will get the expected result by removing that from that line.

An additional note : Convert.ToInt32 will throw FormatException if the input text is not convertible, so use have to use int.TryParse for converting text to integer. so the code will looks like this:

int intTranspo=0,intBoxDaily=0;

if(int.TryParse(textBoxTranspo.Text,out intTranspo) && int.TryParse(textBoxDaily.Text,out intBoxDaily))
   textBoxTotalAmount.Text = (intTranspo + intBoxDaily).ToString();

在语句末尾删除toString

textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text))

Do it like this:

int ans = 0;
ans = Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text);
textBoxTotalAmount.Text = ans.ToString();

EDIT:

This is just one way of creating a clean/neat and more readable code. The way other answered like this

textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text)).ToString();

is also correct but if other programmers will read the code, the 1st one I wrote is more readable and in this case more efficient.

You're getting 1010 because you misplaced the .ToString()

也许应该是:

textBoxTotalAmount.Text = (Convert.ToInt32(textBoxTranspo.Text) + Convert.ToInt32(textBoxDaily.Text)).ToString();

to avoid confusion, first, try to convert the values of the textboxes into int, and put it in respective variables if successful, and output an invalid prompt if not.

int input1 = 0;
int input2 = 0;

try
{
    input1 = Convert.ToInt32(textBoxTranspo.Text);
    input2 = Convert.ToInt32(textBoxDaily.Text); 
    ans = input1 + input2;


    if (!string.IsNullOrEmpty(textBoxTranspo.Text) && !string.IsNullOrEmpty(textBoxDaily.Text))
    {
        textBoxTotalAmount.Text = ans.ToString();
    }

}
catch (Exception)
{
    textBoxTotalAmount.Text = "Invalid input";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM