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
 
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.
 
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
 
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.
 
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]
 
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))
 
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 ;)
 
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:
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 :)
 
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.
 
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 ;)
 
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)
 
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.
 
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.
 
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.
 
Back
Top