Sum of Multiple Text Boxes

  • Thread starter Thread starter Derek
  • Start date Start date
D

Derek

I am trying to add the text of several textboxes as a sum
using the + sign but even converting to integer it is
concatenating. Any help would be greatly appreciated.





Dim price1 As Integer

Dim price2 As Integer

Dim price3 As Integer

Dim price4 As Integer

Dim price5 As Integer

Dim TotalPrice As Integer



price1 = CType(txtcost1.Text, Integer)

price2 = CType(txtcost2.Text, Integer)

price3 = CType(txtcost3.Text, Integer)

price4 = CType(txtcost4.Text, Integer)

price5 = CType(txtcost5.Text, Integer)

TotalPrice = price1 + price2 + price3 + price4 +
price5

txtEC.Text = TotalPrice

ShowFields()
 
Derek:

I just copied your code onto a form and added the five textboxes. I
commented out ShowFields because I don't know what that does. It works
fine. Being that everything here is an int, I'm inclined to think something
else is the problem. you could also try price1.Parse(txtcost1.text) etc but
Ints are going to add not concatenate.

If you debug.WriteLine after the TotalPrice line,
Debug.WriteLine(TotalPrice.ToString), what number do you get?

Also, if any of those text boxes contain nonnumeric data or are empty,
you're going to have some drama, so make sure you don't let the user hit the
Calc button until all the info is good.

Post the rest of the code block, we can figure this one out.

HTH,

Bill

HTH,

Bill
 
What you said about empty strings etc. was right. I was adding the 5th
textbox in code but not showing it to input data.


Thanks so much!

Derek
 
What you said about empty strings etc. was right. I was
adding the 5th
textbox in code but not showing it to input data.


Thanks so much!

Derek
 
Back
Top