Kamil Tomaszewski - 1 year ago 92
Vb.net Question

# How can I add up four numbers from textboxes together and display the answer in a seperate textbox in visual studio?

Visual Basics
In my code I'am getting the user to enter the length and height of each of the four walls. I got the code to multiply the length by the height to work out the area of each wall and display each of the four areas in separate text boxes. Now I want to add up the four numbers that are in the text boxes and display the answer in a separate text box when I click a button.

This is the code that I have at the moment;

``````    Dim txtLength1 As Integer
Dim txtLength2 As Integer
Dim txtLength3 As Integer
Dim txtLength4 As Integer
Dim txtHeight1 As Integer
Dim txtHeight2 As Integer
Dim txtHeight3 As Integer
Dim txtHeight4 As Integer
Dim Area As Integer

Area = TotalArea.Text

txtLength1 = Length1.Text
txtHeight1 = Height1.Text
txtLength2 = Length2.Text
txtHeight2 = Height2.Text
txtLength3 = Length3.Text
txtHeight3 = Height3.Text
txtLength4 = Length4.Text
txtHeight4 = Height4.Text

Area1.Text = txtLength1 * txtHeight1
Area2.Text = txtLength2 * txtHeight2
Area3.Text = txtLength3 * txtHeight3
Area4.Text = txtLength4 * txtHeight4

Area = Area1.Text + Area1.Text + Area3.Text + Area4.Text

End Sub
``````

End Class

When I run this code I get an error saying "Conversion from string "" to type 'Integer' is not valid."

Answer Source

Without taking into account peoples tendency to write other stuff than integers this would do the trick:

``````Dim txtLength1 As Integer
Dim txtLength2 As Integer
Dim txtLength3 As Integer
Dim txtLength4 As Integer
Dim txtHeight1 As Integer
Dim txtHeight2 As Integer
Dim txtHeight3 As Integer
Dim txtHeight4 As Integer
Dim Area As Integer

Area = Convert.ToInt32(TotalArea.Text)

txtLength1 = Convert.ToInt32(Length1.Text)
txtHeight1 = Convert.ToInt32(Height1.Text)
txtLength2 = Convert.ToInt32(Length2.Text)
txtHeight2 = Convert.ToInt32(Height2.Text)
txtLength3 = Convert.ToInt32(Length3.Text)
txtHeight3 = Convert.ToInt32(Height3.Text)
txtLength4 = Convert.ToInt32(Length4.Text)
txtHeight4 = Convert.ToInt32(Height4.Text)

Dim Area1Total As Integer = txtLength1 * txtHeight1
Dim Area2Total As Integer = txtLength2 * txtHeight2
Dim Area3Total As Integer = txtLength3 * txtHeight3
Dim Area4Total As Integer = txtLength4 * txtHeight4

Area1.Text = Area1Total
Area2.Text = Area2Total
Area3.Text = Area3Total
Area4.Text = Area4Total

Area = Area1Ttotal + Area2Total + Area3Total + Area4Total
``````

Edit: Did some minor adjustments in order for you to be able to have the last Area part as it should aswell (since the .Text will make it into a string again).

Edit 2:

If the "TotalArea" Control is not a field that the user should fill in, but rather a sum of the total area as calculated from the various lengths and heights, your code should look like this:

``````Dim txtLength1 As Integer
Dim txtLength2 As Integer
Dim txtLength3 As Integer
Dim txtLength4 As Integer
Dim txtHeight1 As Integer
Dim txtHeight2 As Integer
Dim txtHeight3 As Integer
Dim txtHeight4 As Integer
Dim Area As Integer

txtLength1 = Convert.ToInt32(Length1.Text)
txtHeight1 = Convert.ToInt32(Height1.Text)
txtLength2 = Convert.ToInt32(Length2.Text)
txtHeight2 = Convert.ToInt32(Height2.Text)
txtLength3 = Convert.ToInt32(Length3.Text)
txtHeight3 = Convert.ToInt32(Height3.Text)
txtLength4 = Convert.ToInt32(Length4.Text)
txtHeight4 = Convert.ToInt32(Height4.Text)

Dim Area1Total As Integer = txtLength1 * txtHeight1
Dim Area2Total As Integer = txtLength2 * txtHeight2
Dim Area3Total As Integer = txtLength3 * txtHeight3
Dim Area4Total As Integer = txtLength4 * txtHeight4

Area1.Text = Area1Total
Area2.Text = Area2Total
Area3.Text = Area3Total
Area4.Text = Area4Total

Area = Area1Ttotal + Area2Total + Area3Total + Area4Total
TotalArea.Text = Area
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download