Sum of Multiple Text Boxes

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()
 
W

William Ryan

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
 
D

Derek Mendez

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
 
D

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top