Subtraction from a Textbox

NickJ

Member
Joined
Apr 29, 2005
Messages
21
Programming Experience
Beginner
Hi
Could anyone please help with this .....

Im try to subtract the value of textbox1 from label1.

Then I need to divide label1 by textbox2

Would be easier if i made Label1 into a textbox?




Thanks people

Nick
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,314
Location
Sydney, Australia
Programming Experience
10+
Try something like:

(Integer.Parse(label1.Text) - Integer.Parse(textbox1.Text))/Integer.Parse(textbox2.Text)

You can use the Parse function of other numerical types if your numbers are not integers.
 

NickJ

Member
Joined
Apr 29, 2005
Messages
21
Programming Experience
Beginner
Sorry but that didnt work.

This is what im trying to do...

textbox1 - (2 x Textbox2) and dispaying the answer in lable1

Thanks for your help

Nick
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,314
Location
Sydney, Australia
Programming Experience
10+
Use the same principle like this:

label1.Text = Integer.Parse(textbox1.Text) - 2 * Integer.Parse(textbox2.Text)

Parse() just converts a String to the specified type (Integer, Single, Decimal, etc.) but will throw an exception if the String cannot be converted, so if you haven't validated the values in the TextBoxes you should Catch any exceptions thrown by this line.
 

levyuk

Well-known member
Joined
Jun 7, 2004
Messages
313
Location
Wales, UK
Programming Experience
3-5
use this to make sure only numeric number are entered into the textbox
VB.NET:
[size=2]
[/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] TextBoxContactNumber_KeyPress([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.KeyPressEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] TextBoxContactNumber.KeyPress

[/size][size=2][color=#008000]'Allows only numeric values into the ContactNumber textbox

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] e.KeyChar.IsNumber(e.KeyChar) = [/size][size=2][color=#0000ff]False[/color][/size][size=2] [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]e.Handled = [/size][size=2][color=#0000ff]True

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff]
[/color][/size]
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
CInt and CDec functions

I have used this to convert to integer

label1.Text = CInt(textbox1.Text) - (2 * CInt(textbox2.Text))

Or to decimal:

label1.Text = CDec(textbox1.Text) - (2 * CDec(textbox2.Text))
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
DavidT_macktool said:
I have used this to convert to integer

label1.Text = CInt(textbox1.Text) - (2 * CInt(textbox2.Text))

Or to decimal:

label1.Text = CDec(textbox1.Text) - (2 * CDec(textbox2.Text))
One tiny objection. With all respect, why you are using VB6 functions if vb.NET offers much fuster method CType for the same? maybe it's easy for me as i'm coming from c/c++ background regard programmers that is comming from vb6 background ... but anyway we should tend to use vb.NET ...

i.e.
label1.Text = CType(textbox1.Text, Decimal) - (2 * CType(textbox2.Text, Decimal))

Kind regards ;)
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
actually the CType function should be used for converting larger objects such as well objects

if you're converting from one variable type to another the .Net way is to use Convert

such as
VB.NET:
Label1.Text = Convert.ToString(Convert.ToDecimal(Textbox1.Text) - (2D * Convert.ToDecimal(Textbox2.Text)))
now if you need to convert an object then you should use CType (or DirectCast) such as:
VB.NET:
Dim myTextbox as Textbox = CType(sender, Textbox)
also i've heard (not actually read) that in the final release of vs2005 these functions will no longer be supported:
CInt
CBool
CDec
CStr
etc....
as they do recommend the Convert & CType functions be used in .Net
 
Last edited:

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
JuggaloBrotha said:
............

also i've heard (not actually read) that in the final release of vs2005 these functions will no longer be supported:
CInt
CBool
CDec
CStr
etc....
as they do recommend the Convert & CType functions be used in .Net
I hope you noted that i talked about the same ... Cheers :)
 

DavidT_macktool

Well-known member
Joined
Oct 21, 2004
Messages
502
Location
Indiana
Programming Experience
3-5
Thank You

Thanks Guys,
If I want to correct my code what would you recommend to replace the CInt?
Most of my functionality revolves around plain old part/product quantities and performing basic math operations on them. (Qty * Price), (Qty * QtyPerAssy), (Qty - InventoryQty) etc...

What is my best choice here?

Convert.ToInt16
Convert.ToInt32
Convert.ToInt64

Is Convert.ToInt64 overkill or the safest way to go.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
On a 16-bit machine integers range from -32,768 to +32,767, which is about 2^15 - 1. On a 32-bit machine integers range from -2,147,483,648 to +2,147,483,647, which is about 2^31 - 1. Today 64-bit integers are common. This 64 bit long integer ranges from –9223372036854775808 to 9223372036854775807 (Int is a 32 bit integer, and vb.net's Long is a 64 bit integer; int32 is 31bit + sign, long is 63bit + sign)

Unfortunately, computer integers cannot have those useful commas to mark thousands so, you should be careful when you convert some datatype to INTs.

Cheers ;)
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
Convert.ToInt16 is the Short Variable type
Convert.ToInt32 is the Integer Variable type
Convert.ToInt64 is the Long Variable type

Dim ShrtMyNumber as Short = Convert.ToInt16(Textbox.Text)
 

rpjamess1

New member
Joined
Jul 2, 2005
Messages
3
Programming Experience
Beginner
Pardon a newbie for dropping into a seemingly dead topic, but I'm having a problem with all of these solutions.

What I am trying to do is take a value from 3 textboxes, add them together, then display the answer in a 4th textbox. The method that I have (and works) is as follows:

VB.NET:
Private Sub txtClimbRanks_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClimbRanks.TextChanged
 
		climbRanks = txtClimbRanks.Text
		txtClimbTotal.Text = SetSkillTotal(climbRanks, climbMisc, climbAbility)
 
	End Sub
 
	Private Sub txtClimbMisc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClimbMisc.TextChanged
 
		climbMisc = txtClimbMisc.Text
		txtClimbTotal.Text = SetSkillTotal(climbRanks, climbMisc, climbAbility)
 
	End Sub
 
	Private Sub txtClimbAbility_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClimbAbility.TextChanged
 
		climbAbility = txtClimbAbility.Text
		txtClimbTotal.Text = SetSkillTotal(climbRanks, climbMisc, climbAbility)
 
	End Sub
 
[font=Courier New][color=blue]Private[/color] [color=blue]Function[/color] SetSkillTotal([color=blue]ByVal[/color] pRanks [color=blue]As[/color] [color=blue]String[/color], [color=blue]ByVal[/color] pMisc [color=blue]As[/color] [color=blue]String[/color], [color=blue]ByVal[/color] pAbility [color=blue]As[/color] [color=blue]String[/color])[/font]
 

[font=Courier New]		[color=blue]Dim[/color] intRanks [color=blue]As Integer[/color] = pRanks[/font]
[font=Courier New]		[color=blue]Dim[/color] intMisc [color=blue]As Integer[/color] = pMisc[/font]
[font=Courier New]		[color=blue]Dim[/color] intAbility [color=blue]As Integer[/color] = pAbility[/font]
[font=Courier New]		[color=blue]Dim[/color] intTotal [color=blue][color=blue]As[/color] Integer[/color][/font]

[font=Courier New][color=#0000ff]		[/color][color=black]intTotal = intRanks + intMisc + intAbility[/color][/font]

 
[font=Courier New]		[color=blue]Return[/color] intTotal[/font]
 

 
[font=Courier New]	[color=blue]End[/color] [color=blue]Function[/color][/font]
Now, climbRanks, climbMisc, and climbAbility are declared as Strings at the onset of the form. As I said, this works. However, if I try to avoid the mentioned three variables, and use txtClimbRanks.Text, etc... I get an exception with additional info of "Input String was not in correct format". When I tried each method described above, using the .Text parameter, same error except when using CType, which comes back "Cast from string "" to type "Integer" is not valid".

Any suggestions? I need to run the SetSkillTotal function almost 200 times and would like to avoid creating 200 variables to go along with it. Thanks in advance for any help.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,314
Location
Sydney, Australia
Programming Experience
10+
The Text property of a TextBox is a String. If you want to use it as a number you need to convert it to the correct format, e.g. Integer, Double, Decimal, etc. You should also be validating the input at some point to make sure that the contents of the TextBox is a valid representation of a number. An empty string will cause an exception if you try to convert it to a number, as will leading and trailing spaces, etc. If you want to collect numerical input, you may want to think about using NumericUpDown controls instead of TextBoxes. They are not ideal in all situations but for integers within a range, as I'm guessing you are using, they are ideal. There is no conversion necessary as they have a Value property of type Decimal and all validation is taken care of for you.
 

rpjamess1

New member
Joined
Jul 2, 2005
Messages
3
Programming Experience
Beginner
I've tried converting from String to Integer using CType, Convert.ToInt16, even CInt. None of them work. Each time it returns the "Input String was not in a correct format". I have thought about NumericUpDowns, but some values are set by other contols (and requires an entire redo of the form and control names...which I am willing to do if all else fails).

One code I tried was

intRanks = Convert.ToInt16(pRanks)

It returns the "Input String was not...". This is before the program even runs.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,314
Location
Sydney, Australia
Programming Experience
10+
Like I said, trying to convert an empty string to a number will throw an exception. This is almost certainly what is happening in your case. You need to validate the string to make sure it represents a valid number. You might look into the Double.TryParse method. I would recommend making the changes to use NumericUpDowns, though. In my opinion, they should always be used where they can and only resort to a TextBox if there is some specific reason to. There are valid reasons to use TextBoxes for numerical input, but this usually relates to variable numbers of decimal places. If you're using Integers then the NumericUpDown is usually better. Let's say you are switching from TextBox1 to NumericUpDown1. You can simply do a Find and Replace to switch "TextBox1.Text" to "NumericUpDown1.Value".
 

rpjamess1

New member
Joined
Jul 2, 2005
Messages
3
Programming Experience
Beginner
As I look over my program, the NumericUpDowns do make more sense. Thanks for the help.
 

RichardP

New member
Joined
Jun 6, 2006
Messages
1
Programming Experience
5-10
Use CInt

You should keep CInt. The others were mistaken in saying otherwise. :)

To quote Microsoft's MSDN online help for VB 2005,
"As a rule, you should use the Visual Basic type conversion functions in preference to the .NET Framework methods such as ToString(), either on the Convert class or on an individual type structure or class. The Visual Basic functions are designed for optimal interaction with Visual Basic code, and they also make your source code shorter and easier to read."
( http://msdn2.microsoft.com/en-us/library/s2dy91zy(d=ide).aspx )

As the page makes clear, "Visual Basic type conversion functions" means CInt, CDec, etc, and also CType, DirectCast, TryCast where these are applicable.
 

mafrosis

Well-known member
Joined
Jun 5, 2006
Messages
88
Location
UK
Programming Experience
5-10
What is it with Microsoft and trying to confuse people.

We move from VB6 rubbish to a proper class framework and they keep old functions that dont seem to even be a part of the object heirarchy. And they recommend you use them over the new Convert class! Is it me, or does this just seem really stupid??

It the good old M$ way of training people to do things in such a way that it makes it hard to learn competing language/program/method - the new stuff doesnt make sense because youve learnt things backwards.

They copied all the good stuff from Java, and then threw in a load of crap.

eg,
Dim a(0) As String
Console.Writeline(a.Length)


Result? 1.
Hmmm

And before anyone says it - I know, I should be coding C#.. ;)

</rant>
 

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,015
Location
Cambridge, UK
Programming Experience
5-10
Ok i've has look into this and in my opinion CInt Or Convert.ToInt32 - Who Cares, to say that CInt is the old VB way of doing things is incorrect because you dont have to add a reference to the visualbasic.compatibility namespace to use it. It is therefore part of the object hierachy and a viable entity in vb.net. However they do differ in the way they calculate, for example...

CInt(0.5) will return 0 which should be impossible because 0.5 is not an integer

convert.toint32(0.5) will throw an exception because it correctly determines that 0.5 is not an integer

As for which one to use, i would personnally use CInt because it's faster to type and it highlights in blue so it makes for easier code readability. If that sounds lazy, well then call me lazy but with the amount of typing that all programmers do it is always better to write less code in that regard.
 
Top Bottom