-
Oct 16th, 2014, 02:08 AM
#1
Thread Starter
New Member
[RESOLVED] text box - use value of number for equation
Hi,
I am very new to VB (have 2010) , already love it....
I am just making a small calculator, and it is working in all aspects as intended, except 1 code line...
TextBox4.Text = (TextBox7.Text + TextBox8.Text)
I want the display in textbox4 to show the value of the numbers in textox7 + textbox8
where tb7 is 10 and tb8 is 1, I get a result of 101 , I need a result of 11
I have tried various options, some don't work, but those that do, all show the 101 (which seems 10 + 1 in string, not in numerical value,
could any person please give me some advice on this,
many thanks.
Tim Grindlay. NZ
-
Oct 16th, 2014, 04:22 AM
#2
Re: text box - use value of number for equation
If you want to add numbers then you need numbers to add. The Text property of a TextBox is type String. Just because that String contains numeric characters does not make it a number. You need convert the Strings to a numeric type to perform the arithmetic.
By the way, you should start right now giving all your controls and everything else meaningful names. You should never have TextBox4, TextBox7 and TextBox8 in your code. The variables names should tell you what they are for.
-
Oct 16th, 2014, 04:32 AM
#3
Thread Starter
New Member
Re: text box - use value of number for equation
Hi Jm,
Thanks for the reply, I absolutely love this software... have been doing these things in Excel (which I love), but can be cumbersome for some things...
as it was my first post, the moderator had to review... in the meantime, just tried very simply:
TextBox4.Text = (TextBox7.Text + 1)
worked perfectly -
I have noted the comment re. meaningful names... thanks.... looking forward to more VB (not Victoria bitter)...
-
Oct 16th, 2014, 08:43 AM
#4
Re: [RESOLVED] text box - use value of number for equation
Hi,
That’s still not great and, as already explained, the reason why “TextBox1.Text + TextBox2.Text” did not work is because they were sting values and therefore the strings were concatenated. However, the reason why “TextBox7.Text + 1” did work is because you supplied an integer as the second parameter which meant an Implicit conversion was made to Integer to add the two numbers together.
To get round this in the future and to identify Type conversion errors as they occur then please turn Option Strict ON and never turn it off! This will ensure that you deal with your data type conversions as you program.
As a simple example, and NOT getting into Data Validation at this point, your code should have been correctly written as:-
Code:
CInt(TextBox1.Text) + CInt(TextBox2.Text)
This then forces an Explicit conversion of the TextBox String Types to Integer before you do your arithmetic. Your result would have then been what you expected.
Hope that helps.
Cheers,
Ian
-
Oct 16th, 2014, 11:13 PM
#5
Thread Starter
New Member
Re: [RESOLVED] text box - use value of number for equation
Hi Ian,
Thanks , that does help, great software.. appreciate the pointers... I have located Option Strict and turned on - looking forward to learning more about this.
Tim Grindlay NZ
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|