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

    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
    
asked by Luciano Minetti 30.05.2017 в 01:05
source

1 answer

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 DescuentoSeparado() As String
    Dim Lista As Double = 200
    Dim Dto As Double
    Dim Costo As Double

    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.
    Costo = Lista
    
answered by 31.05.2017 / 15:40
source