| | |
Adding the results of two text boxes (Visual Basic Express Edition)
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
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:
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)
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.
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.
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.
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
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:
would be
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)
Label1.Text = TextBox1.Text + TextBox2.Text
VB.NET Syntax (Toggle Plain Text)
Label1.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Last edited by waynespangler; Oct 17th, 2006 at 6:41 am.
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.
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.
![]() |
Similar Threads
Other Threads in the VB.NET Forum
- Previous Thread: Index was out of range. Must be non-negative and less than the size of the collection
- Next Thread: calendar control
| Thread Tools | Search this Thread |
"crystal .net .net2005 2008 access add advanced application array assignment basic beginner box button buttons center click code combo convert cpu cuesent data database datagrid datagridview designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees excel exists firewall forms html image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity open pan pdf picturebox picturebox2 port print printpreview record regex reports" reuse right-to-left save savedialog search serial socket sorting sqldatbase sqlserver storedprocedure string temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vba vbnet vista visual visualbasic visualbasic.net visualstudio.net web wpf wrapingcode xml





