Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numbers from textboxes are add next to eachother insted of adding them together

I have 3 textboxes in which I write numbers and 1 in which is the result. If I write numbers in TextBoxA and TextBoxB insted of adding them together after I press equal button it put them next to eachother.

[It look like this]

I tried this code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

`

 private void EqualsButton_Click(object sender, EventArgs e)
        {

            OutputTextBox.Text = ($"{InputTextBoxA.Text + InputTextBoxB.Text}");

        }

`

>Solution :

You need to convert text fields to int and after for the answer back to the text.

If you did not enter a value, then there "" is considered as 0 when adding.

private void EqualsButton_Click(object sender, EventArgs e)
{
  OutputTextBox.Text = Convert.ToString(
    Convert.ToInt32(InputTextBoxA.Text == "" ? "0" : InputTextBoxA.Text) 
    + Convert.ToInt32(InputTextBoxB.Text == "" ? "0" : InputTextBoxB.Text));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading