Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add vat into total amount when i add vat into textbox it will change the total amount at text_changed event, but it will give wrong result when i clear one digit from the vat textbox.

So, please help me to solved this.

Below is my sample code that i have tried:

VB
Private Sub txtOutputVat_TextChanged(sender As Object, e As EventArgs) Handles txtOutputVat.TextChanged 
Dim str As Double = 0 
str = CDbl(txtTA.Text + Val((txtTA.Text * txtOutputVat.Text) / 100)) 
txtTA.Text = Val(str) 
str = Nothing 
End Sub


When i press backspace it will not changed the value.

For simplicity here is an example:

We have total amount 3500 and vat amount 10% means 350 Rs into the 3500 Rs. So it will give result 3850 Rs, but when i remove 0 from the vat digit it will increase the total amount and it will give result 3927 Rs.
Posted
Updated 21-Jan-14 7:40am
v3
Comments
Krunal Rohit 21-Jan-14 11:57am    
provide your code here..
MD Chalodiya 21-Jan-14 12:09pm    
Private Sub txtOutputVat_TextChanged(sender As Object, e As EventArgs) Handles txtOutputVat.TextChanged
Dim str As Double = 0
str = CDbl(txtTA.Text + Val((txtTA.Text * txtOutputVat.Text) / 100))
txtTA.Text = Val(str)
str = Nothing
End Sub
when i press backspace it will not changed the value.
Krunal Rohit 21-Jan-14 12:11pm    
What exactly is happening ?
MD Chalodiya 21-Jan-14 12:19pm    
take example we have total amount 3500 and vat amount 10% means 350 rs. into the 3500 rs. so it will give result 3850 rs.
but when i remove 0 from the vat digit but it will increase the total amount and it will give result 3927 rs.

VB
txtTA.Text = Val(str)

As you are setting the value on the TextBox txtTA, so when it calculates inside the event next time, it takes the new value.
VB
str = CDbl(txtTA.Text + Val((txtTA.Text * txtOutputVat.Text) / 100))


Debug your code, you will get a better picture.
 
Share this answer
 
Comments
RaviRanjanKr 21-Jan-14 14:25pm    
5+
Thanks a lot RaviRanjankr. :)
Well yes - of course it will!
Every time you get a change in your vat textbox, you alter the value in the total amount box - which is what you use as the input for the calculation. So when you next make a change to the vat, you can only use the output from the previous calculation as the input to the next!

The proper solution (and by far the easiest) is to have separate subtotal and total boxes: you use the subtotal as the input, and output to the total. You should never try to change the inputs to your calculation!
 
Share this answer
 
I have a couple of suggestion for you, probably it will either fix it or help to fix it.

1) Break down calculation, so you can debug easily, rather than end up with single equation with no clue of it issue.
2) Avoid passing each time to VAL() function its an overhead. rather store it in a variable.
3) As far as i understand, you are probably losing your original value, you should have three textboxes. Lets see how:

- Take a TextBox name as txtActualAmt with a `Text` of 3500
- Take a TextBox name as txtTA with a `Text` of 10 <-- Change the value of this textbox
- Take a TextBox name as txtOutputVat with a `Text` of 3500

Now use this code:

VB
Private Sub txtTA_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTA.TextChanged
    Dim str As Double = 0
    Dim actualAmount As Double = Val(txtActualAmt.Text)
    Dim TA As Double = Val(txtTA.Text)
    Dim percentageAmt As Double = Val(actualAmount * TA) / 100
    str = actualAmount + percentageAmt
    txtOutputVat.Text = str.ToString("0.00")
    str = Nothing
End Sub


Result:

- If TA = 1 , txtOutputVat.Text = 3535.00
- If TA = 10, txtOutputVat.Text = 3850.00
etc..


Notice that:

VB
str = actualAmount + percentageAmt


shows that 'str' is the sum of actual amount and percentage on it.

Hope this is waht you were trying to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900