使用wpf textBox进行数学运算。 [英] Math operations with wpf textBox.

查看:313
本文介绍了使用wpf textBox进行数学运算。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个带三个文本框的WPF窗口



txtPrice



txtTax



txtTotal



我想写txtPrice中的数据并自动填充其他textBoxes。



我写这段代码:



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 :

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



我的代码txtTax vaalue是正确的,是十进制的,但是txtTotal值是不是小数。



例如



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



提前谢谢


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

推荐答案

可能是因为你的例子中的结果是整数。即使你说小数位,Math.Round也不会返回.00,你需要使用格式:



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:

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





您还应验证txtPrice.Text的内容,否则您将获得例外如果有人键入非数字文本。



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


这篇关于使用wpf textBox进行数学运算。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆