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

Time:11-12

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:

`

 private void EqualsButton_Click(object sender, EventArgs e)
        {

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

        }

`

CodePudding user response:

First you must convert text on textbox to int or any number type

Simple way :

    private void EqualsButton2_Click(object sender, EventArgs e)
    {
        int numberA = int.Parse(textBox1.Text.Trim());
        int numberB = int.Parse(textBox2.Text.Trim());

        var result = numberA   numberB;

        textBox3.Text = result.ToString();
    }

Safe way :

    private void EqualsButton_Click(object sender, EventArgs e)
    {
        if (!int.TryParse(textBox1.Text.Trim(), out int numberA))
            numberA = 0;

        if (!int.TryParse(textBox2.Text.Trim(), out int numberB))
            numberB = 0;

        var result = numberA   numberB;

        textBox3.Text = result.ToString();
    }

CodePudding user response:

private void EqualsButton_Click(object sender, EventArgs e)
{
  var resultA=0;
  var resultB=0;
  if(!string.IsNullOrEmpty(InputTextBoxA.Text))
        resultA=Convert.ToInt32(InputTextBoxA.Text);
  if(!string.IsNullOrEmpty(InputTextBoxB.Text))
        resultB=Convert.ToInt32(InputTextBoxB.Text);
    OutputTextBox.Text = resultA  resultB ;
}

CodePudding user response:

You can change into this

 private void EqualsButton_Click(object sender, EventArgs e)
 {
   try
    {
       OutputTextBox.Text = ($"{ Convert.ToInt32(InputTextBoxA.Text.Trim() == "" ? "0" : InputTextBoxA.Text)   Convert.ToInt32(InputTextBoxB.Text.Trim() == "" ? "0" : InputTextBoxB.Text)}");
    }
    catch(exception ex)
    {
      MessageBox.Show("Enter Valid number")'
    }
 }

CodePudding user response:

Don't listen to anyone. Do this

txtResult.Text = string.Empty;

if (!decimal.TryParse(txt1.Text.Trim(), out decimal v1))
{
    MessageBox.Show("Bad value in txt1");
    return;
}
if (!decimal.TryParse(txt2.Text.Trim(), out decimal v2))
{
    MessageBox.Show("Bad value in txt2");
    return;
}

txtResult.Text = (v1   v2).ToString();

CodePudding user response:

InputTextBoxA.Text returns a string instead of an int.

You should do this,

int inputA = int.Parse(InputTextBoxA.Text);
int inputB = int.Parse(InputTextBoxB.Text);
int total = inputA   inputB;
OutputTextBox.Text = total.ToString();

CodePudding user response:

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));
}
  •  Tags:  
  • c#
  • Related