944,029 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 14407
  • VB.NET RSS
Oct 16th, 2006
0

Adding the results of two text boxes (Visual Basic Express Edition)

Expand Post »
I know I'm just missing the mark here, but what I am trying to accomplish is simply adding the results of two text boxes.

Example:
Form has two text boxes and 1 label
The user enters a number (any number from 0 - 10) into each textbox
when the user 'leaves' textbox2, label1 is updated with the total of adding textbox1 and textbox2 together

My Code:
PublicClass Form1
Private Sub TextBox2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.Leave
Label1.Text = TextBox1.Text + TextBox2.Text
End Sub
EndClass


Given my current code, when I enter a 2 in textbox1 and a 3 in textbox2, Label1 updates to show 23 (while I can see WHY that happens, thats not what I was hoping for). I know I'm missing something, just not sure "what". Any help is appreciated. (and yes, I am a VB noob)
Last edited by ChadW; Oct 16th, 2006 at 9:47 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ChadW is offline Offline
42 posts
since Oct 2006
Oct 16th, 2006
0

Re: Adding the results of two text boxes (Visual Basic Express Edition)

You'll need to convert the value of the text fields into numbers before performing the addition operation. Currently you are adding two strings of text, which concatenates them.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Oct 17th, 2006
0

Re: Adding the results of two text boxes (Visual Basic Express Edition)

how would i go about doing that please?
Reputation Points: 10
Solved Threads: 0
Light Poster
ChadW is offline Offline
42 posts
since Oct 2006
Oct 17th, 2006
0

Re: Adding the results of two text boxes (Visual Basic Express Edition)

Search your IDE's help, or failing that Google. These should be your first port of call before a forum. Try the format "[language type]" "[what you are trying to do]". For example "Visual Basic" "convert string to int".
You could even try going through a tutorial, there will be one included with your IDE.
Last edited by DavidRyan; Oct 17th, 2006 at 4:59 am.
Reputation Points: 22
Solved Threads: 11
Posting Whiz in Training
DavidRyan is offline Offline
229 posts
since Jul 2006
Oct 17th, 2006
1

Re: Adding the results of two text boxes (Visual Basic Express Edition)

There are several ways.
1. Dim x As Integer = CInt(TextBox1.Text)
2. Dim y As Integer = Val(TextBox1.Text)
3. Dim z As Integer = CType(TextBox1.Text, Integer)
If you are working with hexidecimal or octal then use val because it knows what &h and &o means. The others throw an error. Also is the only one that if you put it in a label (textbox) then "123.45" will appear as "123.45". If you do as shown above then y is defined as an integer so will only show as "123".
So, I would do this as I would not know what will be placed in the textboxes:
VB.NET Syntax (Toggle Plain Text)
  1. Label1.Text = TextBox1.Text + TextBox2.Text
would be
VB.NET Syntax (Toggle Plain Text)
  1. Label1.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Last edited by waynespangler; Oct 17th, 2006 at 6:41 am.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Oct 17th, 2006
0

Re: Adding the results of two text boxes (Visual Basic Express Edition)

DavidRyan: I just wasn't sure what the proper phrasing was to do a google search, but you are correct in that sense.

Waynespangler: Thank you for that informative example, I will give it a shot. The only things that will be put into the boxes are whole numbers from 0 to 10 (its a bowling score system). Haven't totally decided how to treat the full math aspect of it as yet, but gotta crawl before you walk.
Reputation Points: 10
Solved Threads: 0
Light Poster
ChadW is offline Offline
42 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Index was out of range. Must be non-negative and less than the size of the collection
Next Thread in VB.NET Forum Timeline: calendar control





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC