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

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

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

 
0
  #1
Oct 16th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

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

 
0
  #2
Oct 16th, 2006
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.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

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

 
0
  #3
Oct 17th, 2006
how would i go about doing that please?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 229
Reputation: DavidRyan is an unknown quantity at this point 
Solved Threads: 11
DavidRyan's Avatar
DavidRyan DavidRyan is offline Offline
Posting Whiz in Training

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

 
0
  #4
Oct 17th, 2006
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.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

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

 
1
  #5
Oct 17th, 2006
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:
  1. Label1.Text = TextBox1.Text + TextBox2.Text
would be
  1. Label1.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Last edited by waynespangler; Oct 17th, 2006 at 6:41 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

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

 
0
  #6
Oct 17th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC