如何在vb.net中使用textbox textchanged事件进行添加 [英] How to make addition using textbox textchanged event in vb.net

查看:500
本文介绍了如何在vb.net中使用textbox textchanged事件进行添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将增值税添加到总金额当我将增值税添加到文本框中时,它将更改总金额 text_changed 事件中,但当我从增值税文本框中清除一位数时,它会给出错误的结果。



所以,请帮我解决这个问题。



以下是我试过的示例代码:



 私有  Sub  txtOutputVat_TextChanged(sender 作为 对象,e 作为 EventArgs)句柄 txtOutputVat.TextChanged 
Dim str As Double = 0
str = CDbl (txtTA。 Text + Val((txtTA.Text * txtOutputVat.Text)/ 1 00 ))
txtTA.Text = Val(str)
str = 没什么
结束 Sub





当我按退格它不会改变价值。



为简单起见,这是一个例子:



我们总计金额3500和大桶金额10%意味着350卢比到3500卢比。所以它会给出3850 Rs的结果,但是当我从增值数字中删除0时它将增加总量并且它将得到结果3927 Rs。

解决方案

  txtTA  .Text = Val(str)



当您在上设置值时TextBox txtTA ,所以当它下次在事件内部计算时,它会获取新值。

 str =  CDbl  txtTA  .Text + Val(( txtTA  .Text * txtOutputVat。文本)/  100 ))





调试代码,你会得到更好的图片。


是的 - 当然会的!

每次更改大桶文本框时,都会更改总金额中的值框 - 您用作计算的输入。因此,当您接下来对桶进行更改时,您只能使用之前计算中的输出作为下一个计算的输入!



正确的解决方案(到目前为止最简单的方法)是使用单独的小计和总计框:您使用小计作为输入,并输出到总计。您永远不应该尝试将输入更改为您的计算!


我有一些建议,可能会修复它或帮助修复它。



1)分解计算,这样你就可以轻松调试,而不是最终得到单一的方程式而不知道它的问题。

2)避免每次都通过到 VAL()起到了开销的作用。而是将它存储在一个变量中。

3)据我所知,你可能会失去原来的价值,你应该有三个文本框。让我们看看如何:



- 将TextBox名称改为 txtActualAmt ,其中`Text`为3500

- 将TextBox名称设为 txtTA ,其中Text为10< - 更改此文本框的值

- 将TextBox名称设为 txtOutputVat 带有Text3500 3500



现在使用此代码:



 私有  Sub  txtTA_TextChanged( ByVal  sender 作为 对象 ByVal  e < span class =code-keyword> As  System.EventArgs)句柄 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 = 没什么
结束 Sub





结果:



  - 如果TA = 1,则txtOutputVat.Text = 3535.00 
- 如果TA = 10,则txtOutputVat.Text = 3850.00
等..





请注意:



< pre lang =vb> str = actualAmount + percentageAmt





显示'str'是实际金额和百分比的总和。



希望这是你想要的。


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:

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.

解决方案

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.

str = CDbl(txtTA.Text + Val((txtTA.Text * txtOutputVat.Text) / 100))



Debug your code, you will get a better picture.


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!


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:

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:

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.


这篇关于如何在vb.net中使用textbox textchanged事件进行添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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