# Adding the values of 2 textboxes?

• ### Question

• Hi all,

I'm having trouble adding the value of 2 textboxes and putting the sum into one of the 2 boxes.

So suppose the textboxes are called tbA & tbB my code would be;

`tbA.Text = Val(tbA) + Val(tbB)`

I thought this worked for me before... Now i get a

"System.ArgumentException: 'Argument 'Expression' cannot be converted to type 'TextBox'.'"

I searched for similar questions but no one seems to have asked such a basic question ;)

Monday, August 26, 2019 8:28 PM

• Hi all,

I'm having trouble adding the value of 2 textboxes and putting the sum into one of the 2 boxes.

So suppose the textboxes are called tbA & tbB my code would be;

`tbA.Text = Val(tbA) + Val(tbB)`

I thought this worked for me before... Now i get a

"System.ArgumentException: 'Argument 'Expression' cannot be converted to type 'TextBox'.'"

I searched for similar questions but no one seems to have asked such a basic question ;)

Hi

Try this, it uses the .Text properties of each TextBox instead of the TextBoxes themselves (TextBoxes cannot be used in math operations, only their content)

```    tbA.Text = (Val(tbA.Text) + Val(tbB.Text)).ToString
```

Regards Les, Livingston, Scotland

• Marked as answer by Monday, August 26, 2019 11:14 PM
Monday, August 26, 2019 8:45 PM
• Hello,

You should also check to see if both values can be an integer e.g.

```Dim value1 As Integer = 0
Dim value2 As Integer = 0
If Integer.TryParse(TextBox1.Text, value1) AndAlso Integer.TryParse(TextBox2.Text, value2) Then
TextBox1.Text = (value1 + value2).ToString()
End If```

Same for double and other numerics.

```Dim value1 As Double = 0
Dim value2 As Double = 0
If Double.TryParse(TextBox1.Text, value1) AndAlso Double.TryParse(TextBox2.Text, value2) Then
TextBox1.Text = (value1 + value2).ToString()
End If```

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

• Marked as answer by Tuesday, August 27, 2019 1:15 PM
Monday, August 26, 2019 10:44 PM

### All replies

• Hi all,

I'm having trouble adding the value of 2 textboxes and putting the sum into one of the 2 boxes.

So suppose the textboxes are called tbA & tbB my code would be;

`tbA.Text = Val(tbA) + Val(tbB)`

I thought this worked for me before... Now i get a

"System.ArgumentException: 'Argument 'Expression' cannot be converted to type 'TextBox'.'"

I searched for similar questions but no one seems to have asked such a basic question ;)

Hi

Try this, it uses the .Text properties of each TextBox instead of the TextBoxes themselves (TextBoxes cannot be used in math operations, only their content)

```    tbA.Text = (Val(tbA.Text) + Val(tbB.Text)).ToString
```

Regards Les, Livingston, Scotland

• Marked as answer by Monday, August 26, 2019 11:14 PM
Monday, August 26, 2019 8:45 PM
• Hello,

You should also check to see if both values can be an integer e.g.

```Dim value1 As Integer = 0
Dim value2 As Integer = 0
If Integer.TryParse(TextBox1.Text, value1) AndAlso Integer.TryParse(TextBox2.Text, value2) Then
TextBox1.Text = (value1 + value2).ToString()
End If```

Same for double and other numerics.

```Dim value1 As Double = 0
Dim value2 As Double = 0
If Double.TryParse(TextBox1.Text, value1) AndAlso Double.TryParse(TextBox2.Text, value2) Then
TextBox1.Text = (value1 + value2).ToString()
End If```

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

• Marked as answer by Tuesday, August 27, 2019 1:15 PM
Monday, August 26, 2019 10:44 PM
• Perfect, super. That was exactly the problem, i needed to add the contents of the boxes--not the boxes.

Many thanks

Monday, August 26, 2019 11:16 PM
• Interesting thanks
Monday, August 26, 2019 11:18 PM
• Interesting thanks
I would not call it interesting but instead how professionals perform this type of operation.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Monday, August 26, 2019 11:20 PM