RIAS
asked on
Total in textboxes
Hello,
I am using vb.net 2015 desktop application.
I have 6 textboxes in which one will be occupied with an number .This textbox will be treated as a total amount.
The requirement is if the user adds any amount of value in the rest of the 5 textboxes the amount should get deducted from the 'total amount.' textbox.
total amount textbox can be any one the textboxes present on the form.
Any suggestions on how can i achieve it .
I am using vb.net 2015 desktop application.
I have 6 textboxes in which one will be occupied with an number .This textbox will be treated as a total amount.
The requirement is if the user adds any amount of value in the rest of the 5 textboxes the amount should get deducted from the 'total amount.' textbox.
total amount textbox can be any one the textboxes present on the form.
Any suggestions on how can i achieve it .
ASKER
Thanks Anil ,
I am using vb.net .Can we have code in vb.net please?
I am using vb.net .Can we have code in vb.net please?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Sir!
ASKER
Hello Saige Sir,
It works great, but if I keep chaging my numbers in other textbox ,it goes in negative. Is there any way the total textbox should stop at 0 and respond to the changes done in the amount in the other textboxes?
It works great, but if I keep chaging my numbers in other textbox ,it goes in negative. Is there any way the total textbox should stop at 0 and respond to the changes done in the amount in the other textboxes?
Either check the calculation result before setting the textbox text; e.g. -
As for the second part of your follow-up question, I don't understand what you mean by "respond to the changes done in the amount in the other textboxes".
-saige-
Private Sub OnValidated(sender As Object, e As EventArgs) Handles TextBox6.Validated, TextBox5.Validated, TextBox4.Validated, TextBox3.Validated, TextBox2.Validated, TextBox1.Validated
If TypeOf sender Is TextBox Then
Dim tb = DirectCast(sender, TextBox)
If _total IsNot Nothing AndAlso Not _total.Equals(tb) Then
If Not String.IsNullOrWhiteSpace(tb.Text) Then
Dim temp = CDbl(_total.Text) - CDbl(tb.Text)
_total.Text = If(temp < 0, 0, temp)
End If
End If
End If
End Sub
Or check the textbox text to see if it is less than zero after setting; e.g. -
Private Sub OnValidated(sender As Object, e As EventArgs) Handles TextBox6.Validated, TextBox5.Validated, TextBox4.Validated, TextBox3.Validated, TextBox2.Validated, TextBox1.Validated
If TypeOf sender Is TextBox Then
Dim tb = DirectCast(sender, TextBox)
If _total IsNot Nothing AndAlso Not _total.Equals(tb) Then
If Not String.IsNullOrWhiteSpace(tb.Text) Then
_total.Text = CDbl(_total.Text) - CDbl(tb.Text)
If CDbl(_total.Text) < 0 Then _total.Text = 0
End If
End If
End If
End Sub
As for the second part of your follow-up question, I don't understand what you mean by "respond to the changes done in the amount in the other textboxes".
-saige-
ASKER
Thanks Sir,
Sorry for the confusion
"respond to the changes done in the amount in the other textboxes".
Will try to work on your solution.
Cheers!
Sorry for the confusion
"respond to the changes done in the amount in the other textboxes".
Will try to work on your solution.
Cheers!
ASKER
Sir,
if I add value to the textbox ,it gets deducted from the total but, if I remove the value from textbox it does not get added back to to the total.
example:
1. total textbox textbox1
1000
2. total textbox textbox1
900 100
3. total textbox textbox1
900 -------expected result is it should go back to 1000 but does not
Any suggestions Sir?
Cheers
if I add value to the textbox ,it gets deducted from the total but, if I remove the value from textbox it does not get added back to to the total.
example:
1. total textbox textbox1
1000
2. total textbox textbox1
900 100
3. total textbox textbox1
900 -------expected result is it should go back to 1000 but does not
Any suggestions Sir?
Cheers
ASKER
it worked perfectly with few tweaks! Thanks
Open in new window