# Calculate formula in textbox in VB.NET

1

I have three TextBox.

LISTATextBox, DESCUENTOTextBox and COSTOTextbox.

`COSTOTextbox` would be equal to `LISTATextbox + DESCUENTOTextBox` .

My problem is that in DESCUENTOTextBox I would have to place a formula like the following: `10%-20%+5%` .

They are always operations with `%` . And always add or rest the percentages.

• Example: if in LIST I have `100` and in DISCOUNT `-10%-10%` , COST should result in `81` .

Is there a way to solve it? Until now I could only work with simple accounts, but not chain formulas.

I leave the code I have so far:

``````Private Sub DESCUENTOTextBox_TextChanged(sender As Object, e As EventArgs) Handles DESCUENTOTextBox.TextChanged

Dim Numero As String
Dim math As String
math = Val(DESCUENTOTextBox.Text)
Numero = LISTATextBox.Text + DESCUENTOTextBox.Text
COSTOTextBox.Text = Numero

End Sub
``````

---- EDIT --------------------------------------- -----------------------

I leave the final code with which I could solve the problem. Thank you, Agustin M.!

``````Private Sub DESCUENTOTextBox_TextChanged(sender As Object, e As EventArgs) Handles DESCUENTOTextBox.TextChanged

Dim Descuento As String = DESCUENTOTextBox.Text

If LISTATextBox.Text = "" Then
LISTATextBox.Text = "0"
End If
If LISTATextBox.Text > "0" Then
Dim Lista As Double = LISTATextBox.Text

For x = 0 To DescuentoSeparado.Length - 1
' Recorremos el Array y dependiendo si el descuento es
' positivo o negativo se lo sumamos o restamos a Lista
Lista = Lista + (Lista * Val(DescuentoSeparado(x)) / 100)
Else
Lista = Lista - (Lista * Math.Abs(Val(DescuentoSeparado(x))) / 100)
End If
End If
Next

' Finalmente obtenemos Costo.
COSTOTextBox.Text = Lista
End If
If LISTATextBox.Text = "0" Then
COSTOTextBox.Text = "0"
End If

End Sub
``````

asked by Luciano Minetti 30.05.2017 в 01:05
source

2

Your problem can be solved by separating the chain that has the discounts with the percentage character (% ). Then we can go through the `array` that results from that to apply the discounts to the List price, finally leaving a cost.

Example Code:

``````    Dim Descuento As String = "10%-20%+5%"

Dim Lista As Double = 200
Dim Dto As Double
Dim Costo As Double

For x = 0 To DescuentoSeparado.Length - 1
' Recorremos el Array y dependiendo si el descuento es
' positivo o negativo se lo sumamos o restamos a Lista