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 in81
.
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
Dim DescuentoSeparado() As String
If LISTATextBox.Text = "" Then
LISTATextBox.Text = "0"
End If
If LISTATextBox.Text > "0" Then
Dim Lista As Double = LISTATextBox.Text
DescuentoSeparado = Descuento.Split("%")
'En DescuentoSeparado almacenamos el Array separado por %
For x = 0 To DescuentoSeparado.Length - 1
If DescuentoSeparado(x).ToString <> "" Then
' Recorremos el Array y dependiendo si el descuento es
' positivo o negativo se lo sumamos o restamos a Lista
If Val(DescuentoSeparado(x)) > 0 Then
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