Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

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 .
Avatar of Anil Golamari
Anil Golamari
Flag of United States of America image

Below is sample code that works for me.  

<form id="form1" runat="server">

 <script type="text/javascript">
        function sum() {
            var txtFirstNumberValue = document.getElementById('TextBox1').value;
            var txtSecondNumberValue = document.getElementById('TextBox2').value;
            var txtThridNumberValue = document.getElementById('TextBox3').value;
            var txtFourthNumberValue = document.getElementById('TextBox4').value;
            var txtFifthNumberValue = document.getElementById('TextBox5').value;
 
            if (txtFirstNumberValue == "")
                txtFirstNumberValue = 0;
 
            if (txtSecondNumberValue == "")
                txtSecondNumberValue = 0;
 
            if (txtThridNumberValue == "")
                txtThridNumberValue = 0;
 
            if (txtFourthNumberValue == "")
                txtFourthNumberValue = 0;
            if (txtFifthNumberValue == "")
                txtFifthNumberValue = 0;
 
            var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue) + parseInt(txtThridNumberValue) + parseInt(txtFourthNumberValue) + parseInt(txtFifthNumberValue);
            if (!isNaN(result)) {
                document.getElementById('TextBox6').value = result;
            }
        }
</script>


<div>

    <asp:TextBox ID="TextBox1" runat="server" onkeyup="sum();"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox2" runat="server" onkeyup="sum();"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox3" runat="server" onkeyup="sum();"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox4" runat="server" onkeyup="sum();"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox5" runat="server" onkeyup="sum();"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
    <br />

</div>
</form>

Open in new window

Avatar of RIAS

ASKER

Thanks Anil ,

I am using vb.net .Can we have code in vb.net please?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Thank you Sir!
Avatar of RIAS

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?
Either check the calculation result before setting the textbox text; 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
				Dim temp = CDbl(_total.Text) - CDbl(tb.Text)
				_total.Text = If(temp < 0, 0, temp)
			End If
		End If
	End If
End Sub

Open in new window

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

Open in new window


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-
Avatar of RIAS

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!
Avatar of RIAS

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
Avatar of RIAS

ASKER

it worked perfectly with few tweaks! Thanks