Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi guys

I have a WPF Window with three textBoxes

txtPrice

txtTax

txtTotal

I want to write data in txtPrice and automatically fill the others textBoxes.

I write this code :

VB
Private Sub txtPrice_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
        Dim num1 As Decimal
        num1 = CDec(Val(txtPrice.Text) * 4 / 100)
        txtTax.Text = Math.Round(num1, 2)
    End Sub

    Private Sub txtTax_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
        Dim num2 As Decimal
        num2 = CDec(Val(txtPrice.Text) + CDec(Val(txtTax.Text))
        txtTotal.Text = Math.Round(num1, 2)
    End Sub

with my code txtTax vaalue is right and is decimal but txtTotal value is not a decimal.

e.g

txtPrice.Text = 534,46 txtTax.Text = 21,37 txtTotal.Text = 555

Thanks in advance
Posted
Updated 24-Jan-16 3:05am
v3
Comments
[no name] 24-Jan-16 9:20am    
I don't know VB and I did not look in Details to your Problem. But on a first glance " * 4 / 100" is maybe the problem. Try "* 4.0 / 100.0" or what I usually do: "* 4.0 * 0.01".
Sergey Alexandrovich Kryukov 24-Jan-16 13:54pm    
Why do you use Round at all?
—SA

1 solution

Probably because the result in your example is a whole number. Math.Round will not return .00 even though you state decimal places, you need to use formatting:

VB
txtTotal.Text = Math.Round(num1,2).Tostring("0.00",CultureInfo.InvariantCulture)


You should also verify the content of the txtPrice.Text or you'll get exceptions if someone types non-numeric text.
 
Share this answer
 
v3

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