[RESOLVED] [02/03] Easy textbox question
Hi people,
just a simple question really. I have two textboxes, textbox 2 has a number in there which is taken from a database. In textbox1 I want to input a number, however I want to take that number away from the total in textbox2.
For example if textbox2 has 10 in there, and I input 2 in textbox1 I want the number in textbox 2 to show 8.
Is this possible? thanks.
Re: [02/03] Easy textbox question
Quote:
For example if textbox2 has 10 in there, and I input 2 in textbox1 I want the number in textbox 2 to show 8
Here's the general idea - you need to cast the textbox values, which are of string data types, into numerical values to perform the calculation.
Code:
TextBox2.Text = (ctype(TextBox2.Text, integer) - ctype(Textbox1.Text, integer)).ToString
Side notes:- You will need to wrap validation code around this to check the textboxes have values within them, and also that these are numerical values.
- You will need to let us know if you need decimal points or have very large or scientific numbers as we will then be able to suggest an appropriate replacement data type than the Integer in this sample
- I'm using CType here as the Convert() or .ToInt32/.ToInt64 restrict the compilation to a 32-bit or 64-bit architecture in the coding, whereas the use of CType or DirectCast allow you to specify just Integer and choose the architecture then at compile time
Re: [02/03] Easy textbox question
Thanks for your help:
1, could you help me on the validation please? There is only two types of validation required, 1: only numeric values are allowed in textbox1 and 2: when I hit the backspace now it breaks on the line and gives me the following error:
Additional information: Cast from string "" to type 'Integer' is not valid.
If I hit the back space I want the value in textbox2 to change to the original value, is that possible?
2, there are no decimal places in the numbers and the number do not have to be rounded up. I wont be using large numbers and the numbers will be under 100.
Thanks
Re: [02/03] Easy textbox question
The exception message you are getting is because there's no validation - you delete the number in textbox 1 resulting in an empty string value, which cannot be cast to a numerical value attempted to be subtracted from the cast numerical textbox2 value.
Try this one then :) and let us know if this is what you're after here:
Code:
Public Class Form1
Private _originalTextboxValue As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Population of your textbox2 control. In reality this
' would be following your database code retreival.
TextBox2.Text = 10
_originalTextboxValue = 10
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim textbox1Number As Integer
Dim textbox2Number As Integer
If (String.IsNullOrEmpty(TextBox1.Text)) Then
TextBox2.Text = _originalTextboxValue
ElseIf (Not String.IsNullOrEmpty(TextBox2.Text)) Then
Integer.TryParse(TextBox1.Text.Trim, textbox1Number)
Integer.TryParse(TextBox2.Text.Trim, textbox2Number)
If (Not textbox1Number.Equals(New Integer)) AndAlso _
(Not textbox2Number.Equals(New Integer)) Then
TextBox2.Text = (textbox2Number - textbox1Number).ToString
End If
End If
End Sub
End Class
Re: [02/03] Easy textbox question
Re: [02/03] Easy textbox question
I expect you may want to change the line
Code:
TextBox2.Text = (_textbox2Number- textbox1Number).ToString
to this instead
Code:
TextBox2.Text = (_originalTextboxValue - textbox1Number).ToString
In the code posted above there, entering a 3 into textbox1 renders the result shown within the text of textbox2 to be 7 (10-3). Entering a following 0 character into textbox1, therefore giving a value of 30 entered then performs a second calculation taking the current value of 7 in textbox 2, minus 30 (giving -23).
If you wanted all numbers to be subtracted from the original value of textbox2 (so 10-3=7 and 10-30=-20 as opposed to 10-3=7, 7-30=-23) then you just need to alter that line above. But this should provide you with both options and I hope is of help to resolve your issue?