none
How Do I Takeaway a Percentage from a Value. RRS feed

  • Question

  • Hi Good People.

    I have a form with Textboxes 

    TextBox1 = txtEnterNoItems ( This is for entering the Amount of Items)

    Textbox2 = txtEntertDiscount ( This is For Setting the Percentage Rate)

    Textbox3 = txtSubTotal (This is for the Pre-Value of Items before Tax)

    Textbox4 = txtInsertPrice ( This is for inserting the price of one item)

    My Question is How do I Minus a Percentage from Textbox3, From a Percentage Value in Texbox2.

    Without using textbox2 Everything works fine using a value of (0) in textbox2. I am looking for the formula the enables me to minus the Percentage from the Sub Total Textbox (Textbox3) Using the Percentage Value in Textbox2. 

    This will be in a button click event.   Code I have is Below..

     Private Sub cmdCalculateItems_Click(sender As Object, e As EventArgs) Handles cmdCalculateItems.Click
            If IsNumeric(txtEnterNoItems.Text) Then
    
                Dim i As Double = txtEnterNoItems.Text
                i -= txtEntertDiscount.Text
                txtSubTotal.Text = i.ToString
                txtSubTotal.Text = FormatCurrency(txtSubTotal.Text)
    
                txtVAT1.Text = txtSubTotal.Text * Val(txtVATRate.Text)
    
                Dim Sum1 As Double = txtInsertPrice.Text
                Dim Sum2 As Double = txtEnterNoItems.Text
                Dim Sum3 As Double = txtSubTotal.Text
                Dim Sum4 As Double = txtVATRate.Text
    
    
                i *= txtVATRate.Text
                txtVAT1.Text = i.ToString
                txtVAT1.Text = FormatCurrency(txtVAT1.Text)
    
                'txtTotal1.Text = (Sum1 + Sum3) * Sum2
                txtSubTotal.Text = Sum1 * Sum2
                txtVAT1.Text = Sum2 + Sum3 * Sum4
                txtTotal1.Text = Sum3 + Sum4
    
                txtSubTotal.Text = FormatCurrency(txtSubTotal.Text)
                txtVAT1.Text = FormatCurrency(txtVAT1.Text)
                'txtTotal1.Text = FormatCurrency(txtTotal1.Text)
    
                lbVatRateUsed.Text = txtVATRate.Text & " %"
                lbPricePerItem.Text = txtInsertPrice.Text
                lbAmountOfItems.Text = txtEnterNoItems.Text
    
                lbSubTotal.Text = txtSubTotal.Text
                lbVAT1.Text = txtVAT1.Text
    
                If txtEntertDiscount.Text = "0" Then
                    lbDiscountUsed.Text = "No Discount Used"
                Else
                    lbDiscountUsed.Text = txtEntertDiscount.Text & " %"
    
                End If
    
            Else
                MessageBox.Show("Enter a valid Number", "VAT System", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
    
            cmdCalculateVAT1.PerformClick()
    
        End Sub

    I hope you can understand my question?

    Kind Regards Gary


    Gary Simpson

    Saturday, January 12, 2019 5:14 PM

Answers

  • Hi

    OK, I think I understand.

    Here is a stand alone example, using a little bit more than yours, splitting it down to more steps that could be easier to follow.

    Note: it can be benificial, knowing you are intending to do math operations on TextBox contents, to add a couple of useful functions. These, (GetInteger and GetDouble) are used any time a numerical value is needed from a string(TextBox.Text) and simplifies that side of things somewhat. (and, less error prone where they will just return Zero if non numeric entry in String)

    I have marked the line(s) that I believe tou are asking for.

    As tjhis is a stand alone, maybe try it out separately and see if it helps.

    Image

    Code

    ' Form1 with 7 textboxes (some
    ' are disabled as they are for display
    ' 7 description labels and Button1 (CALC)
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' using strings you need to convert
        ' all to numeric for calculations
        Dim cost As Double = GetDouble(txtCostPerItem.Text)
    
        Dim num As Integer = GetInteger(txttxtEnterNoItems.Text)
    
        Dim disc As Double = GetDouble(txtEntertDiscount.Text)
    
        ' this may be the line that you want
        ' ----------------------
        Dim sub1 As Double = num * cost
        txtSubTotal.Text = (sub1 - (sub1 / 100 * disc)).ToString
        ' ----------------------
    
        Dim tax As Double = GetDouble(txtVATRate.Text)
    
        txtTaxAmount.Text = (GetDouble(txtSubTotal.Text) / 100 * tax).ToString
    
        GrandTotal.Text = (GetDouble(txtSubTotal.Text) + GetDouble(txtTaxAmount.Text)).ToString
    
      End Sub
      Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return 0
      End Function
      Function GetDouble(s As String) As Double
        Dim v As Double = 0.0
        If Double.TryParse(s, v) Then Return v
        Return 0.0
      End Function
    
    
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Gary Simpson Saturday, January 12, 2019 7:29 PM
    Saturday, January 12, 2019 6:49 PM
  • Hi

    Herte is a quickly done alternative based on your Designed Form and the code I previously posted..

    It probably doesn't conform exactly to your needs, but seems to be close. It is quite a bit more compact than yours (which means nothing at all).

    I just did this by expanding on the code I posted earlier and adapted it to suit your Designed Form and Control names etc.

    If you want to try this out. Do it on a COPY of your project, copy/replace the Form1 code with this.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cmdCalculateItems.Enabled = False
      End Sub
      Private Sub cmdCalculateItems_Click(sender As Object, e As EventArgs) Handles cmdCalculateItems.Click
        ' using strings you need to convert
        ' all to numeric for calculations
        Dim cost As Double = GetDouble(txtInsertPrice.Text)
    
        Dim num As Integer = GetInteger(txtEnterNoItems.Text)
    
        Dim disc As Double = GetDouble(txtEntertDiscount.Text)
    
        ' this may be the line that you want
        ' ----------------------
        Dim sub1 As Double = num * cost
        Dim sub2 As Double = sub1 - (sub1 / 100 * disc)
        txtSubTotal.Text = sub2.ToString("£0.00")
        ' ----------------------
    
        txtTotalDiscount.Text = (sub1 / 100 * disc).ToString("£0.00")
    
        Dim tax As Double = GetDouble(txtVATRate.Text)
    
        txtVAT1.Text = (sub2 / 100 * tax).ToString("£0.00")
    
        txtTotal1.Text = (sub2 + sub2 / 100 * tax).ToString("£0.00")
    
        lbVatRateUsed.Text = lbVATRate.Text
        lbPricePerItem.Text = lbAmountEntered.Text
        lbAmountOfItems.Text = txtEnterNoItems.Text
        If disc = 0 Then
          lbDiscountUsed.Text = "No Discount Used"
        Else
          lbDiscountUsed.Text = txtEntertDiscount.Text & "%"
        End If
        lbDiscountAmount.Text = txtTotalDiscount.Text
        lbSubTotal.Text = sub1.ToString("£0.00")
        lbVAT1.Text = txtVAT1.Text
        lbTotal1.Text = txtTotal1.Text
      End Sub
      Private Sub cmdCalculateVAT_Click(sender As Object, e As EventArgs) Handles cmdCalculateVAT.Click
        Dim VATrate As Double = GetDouble(txtVATRate.Text)
        If VATrate < 0 Then
          MessageBox.Show("VAT Rate  can not be negative")
          txtVATRate.Text = Nothing
          Exit Sub
        End If
    
        Dim UnitPrice As Double = GetDouble(txtInsertPrice.Text)
        If UnitPrice < 0 Then
          MessageBox.Show("UnitPrice can not be negative")
          txtInsertPrice.Text = Nothing
          Exit Sub
        End If
    
        txtVAT.Text = (UnitPrice / 100 * VATrate).ToString("£0.00")
        txtTotal.Text = (UnitPrice + UnitPrice / 100 * VATrate).ToString("£0.00")
    
        lbVATRate.Text = txtVATRate.Text & "%"
        lbAmountEntered.Text = GetDouble(txtInsertPrice.Text).ToString("£0.00")
        lbVatTotal.Text = txtVAT.Text
        lbTotal.Text = txtTotal.Text
    
        cmdCalculateItems.Enabled = True
    
      End Sub
      Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return 0
      End Function
      Function GetDouble(s As String) As Double
        Dim v As Double = 0.0
        If Double.TryParse(s, v) Then Return v
        Return 0.0
      End Function
      Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
        Close()
      End Sub
      Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
        For Each c As Control In Controls
          If c.GetType() Is GetType(TextBox) Then
            c.Text = Nothing
          End If
        Next
        For Each c As Control In Panel1.Controls
          If c.GetType() Is GetType(Label) Then
            If c.Name.StartsWith("lb") Then c.Text = "0"
          End If
        Next
        For Each c As Control In Panel2.Controls
          If c.GetType() Is GetType(Label) Then
            If c.Name.StartsWith("lb") Then c.Text = "0"
          End If
        Next
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Gary Simpson Wednesday, January 16, 2019 1:58 PM
    Monday, January 14, 2019 11:22 PM

All replies

  • Hi

    Would you kindly re-write (or comment) your post to include the control names - such as TextBox1 (txtVATRate) etc

    It is one thing to assist, but another to try and sort out which controls are being discussed if the text doesn't match up with the code.


    Regards Les, Livingston, Scotland

    Saturday, January 12, 2019 5:31 PM
  • Hi

    Would you kindly re-write (or comment) your post to include the control names - such as TextBox1 (txtVATRate) etc

    It is one thing to assist, but another to try and sort out which controls are being discussed if the text doesn't match up with the code.


    Regards Les, Livingston, Scotland

    How Do I Minus A Percentage from Textbox3( txtSubTotal) with the Value of  Textbox2 (txtEntertDiscount). 

    Sorry Les Again I was over thinking the question and was trying to put as much information as possible down.

    Regards Gary


    Gary Simpson

    Saturday, January 12, 2019 5:53 PM
  • Les, Maybe this is all the code  I should have shown that I am stuck on...

    If txtEntertDiscount.Text = "0" Then
                    lbDiscountUsed.Text = "No Discount Used"
                Else
                    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    
                    ' Here i where I am Stuck. 
                    'If txtEnterDiscount.text is greater that "0", ie in (txtEntertDiscount) And I enter the value 0.2 this 
                    'represents 20%, How Do I take away the value of 20% from the Sub Total (txtSubTotal)
    
    
                    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    
                    lbDiscountUsed.Text = txtEntertDiscount.Text & " %"
    
                End If
    
            Else
                MessageBox.Show("Enter a valid Number", "VAT System", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If


    Gary Simpson

    Saturday, January 12, 2019 6:25 PM
  • Hi

    OK, I think I understand.

    Here is a stand alone example, using a little bit more than yours, splitting it down to more steps that could be easier to follow.

    Note: it can be benificial, knowing you are intending to do math operations on TextBox contents, to add a couple of useful functions. These, (GetInteger and GetDouble) are used any time a numerical value is needed from a string(TextBox.Text) and simplifies that side of things somewhat. (and, less error prone where they will just return Zero if non numeric entry in String)

    I have marked the line(s) that I believe tou are asking for.

    As tjhis is a stand alone, maybe try it out separately and see if it helps.

    Image

    Code

    ' Form1 with 7 textboxes (some
    ' are disabled as they are for display
    ' 7 description labels and Button1 (CALC)
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' using strings you need to convert
        ' all to numeric for calculations
        Dim cost As Double = GetDouble(txtCostPerItem.Text)
    
        Dim num As Integer = GetInteger(txttxtEnterNoItems.Text)
    
        Dim disc As Double = GetDouble(txtEntertDiscount.Text)
    
        ' this may be the line that you want
        ' ----------------------
        Dim sub1 As Double = num * cost
        txtSubTotal.Text = (sub1 - (sub1 / 100 * disc)).ToString
        ' ----------------------
    
        Dim tax As Double = GetDouble(txtVATRate.Text)
    
        txtTaxAmount.Text = (GetDouble(txtSubTotal.Text) / 100 * tax).ToString
    
        GrandTotal.Text = (GetDouble(txtSubTotal.Text) + GetDouble(txtTaxAmount.Text)).ToString
    
      End Sub
      Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return 0
      End Function
      Function GetDouble(s As String) As Double
        Dim v As Double = 0.0
        If Double.TryParse(s, v) Then Return v
        Return 0.0
      End Function
    
    
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Gary Simpson Saturday, January 12, 2019 7:29 PM
    Saturday, January 12, 2019 6:49 PM
  • Hi Les

    Thank you very much for sorting the problem out for me, I didn't know if or where to use the forward slash I was trying to use the Plus Minus, Something like  Textbox1 .text -* Textbox2 with an total entered into texbox3. But I kept on getting an error. 

    Thanks Again Les.

    Regards 

    Gary


    Gary Simpson

    Saturday, January 12, 2019 7:29 PM
  • Hi Les

    Would it be Possible for you to have a look at my VAT Calculator. And tell me What I have done wrong. Because I really want to learn this...

    I have Zipped up the Program And shared it on One Drive. Hopefully you can Download the Program Called …..

    VAT Calculator. Zip   Here is the Code From One Drive... https://1drv.ms/u/s!ApNyZJF4iLAo6XQ1Mpx9MOyLcSiu

    Regards Gary


    Gary Simpson

    Monday, January 14, 2019 8:17 PM
  • Hi Gary

    I have had a brief look at your application. I had to comment out the 'cmdCalculateItems.Enabled = False' line to get it to be available (when I first tried, it rremained Disabled?)

    Having tried it, it all seems to work OK for me.

    What issues did you have?

    Here is an image of one test. The final VAT of £9.00 and final charge of £99.00 seem correct to me.


    Regards Les, Livingston, Scotland

    Monday, January 14, 2019 9:03 PM
  • Hi Les,

    I just wondered If I should have written the code Differently that was all...

    Thanks for checking my and your code...

    Regards Gary 


    Gary Simpson

    Monday, January 14, 2019 9:23 PM
  • Hi Les,

    I just wondered If I should have written the code Differently that was all...

    Thanks for checking my and your code...

    Regards Gary 


    Gary Simpson

    Hi

    No problem. Well done getting so far :)

    One thing I would stress: you would benefit greatly if you add the two Option lines at the very top of your code

    Option Strict On
    Option Explicit On
    Public Class Form1
    
      Private Sub cmdCalculateVAT_Click(sender As Object, e As EventArgs) Handles cmdCalculateVAT.Click
    ................... etc etc etc

    Doing so at this late stage will indicate a bunch of errors. These errors are only due to the strictness imposed by the Options, but, will help prevent many possible run time issues (the kind of thing where you try running from within the Editor and all is fine but errors show up after publication)

    If you include those lines before starting main coding, then many of these will be indicated as you go and so are not cumbersome at all.


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, January 14, 2019 9:34 PM
    Monday, January 14, 2019 9:33 PM
  • Hi

    Herte is a quickly done alternative based on your Designed Form and the code I previously posted..

    It probably doesn't conform exactly to your needs, but seems to be close. It is quite a bit more compact than yours (which means nothing at all).

    I just did this by expanding on the code I posted earlier and adapted it to suit your Designed Form and Control names etc.

    If you want to try this out. Do it on a COPY of your project, copy/replace the Form1 code with this.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cmdCalculateItems.Enabled = False
      End Sub
      Private Sub cmdCalculateItems_Click(sender As Object, e As EventArgs) Handles cmdCalculateItems.Click
        ' using strings you need to convert
        ' all to numeric for calculations
        Dim cost As Double = GetDouble(txtInsertPrice.Text)
    
        Dim num As Integer = GetInteger(txtEnterNoItems.Text)
    
        Dim disc As Double = GetDouble(txtEntertDiscount.Text)
    
        ' this may be the line that you want
        ' ----------------------
        Dim sub1 As Double = num * cost
        Dim sub2 As Double = sub1 - (sub1 / 100 * disc)
        txtSubTotal.Text = sub2.ToString("£0.00")
        ' ----------------------
    
        txtTotalDiscount.Text = (sub1 / 100 * disc).ToString("£0.00")
    
        Dim tax As Double = GetDouble(txtVATRate.Text)
    
        txtVAT1.Text = (sub2 / 100 * tax).ToString("£0.00")
    
        txtTotal1.Text = (sub2 + sub2 / 100 * tax).ToString("£0.00")
    
        lbVatRateUsed.Text = lbVATRate.Text
        lbPricePerItem.Text = lbAmountEntered.Text
        lbAmountOfItems.Text = txtEnterNoItems.Text
        If disc = 0 Then
          lbDiscountUsed.Text = "No Discount Used"
        Else
          lbDiscountUsed.Text = txtEntertDiscount.Text & "%"
        End If
        lbDiscountAmount.Text = txtTotalDiscount.Text
        lbSubTotal.Text = sub1.ToString("£0.00")
        lbVAT1.Text = txtVAT1.Text
        lbTotal1.Text = txtTotal1.Text
      End Sub
      Private Sub cmdCalculateVAT_Click(sender As Object, e As EventArgs) Handles cmdCalculateVAT.Click
        Dim VATrate As Double = GetDouble(txtVATRate.Text)
        If VATrate < 0 Then
          MessageBox.Show("VAT Rate  can not be negative")
          txtVATRate.Text = Nothing
          Exit Sub
        End If
    
        Dim UnitPrice As Double = GetDouble(txtInsertPrice.Text)
        If UnitPrice < 0 Then
          MessageBox.Show("UnitPrice can not be negative")
          txtInsertPrice.Text = Nothing
          Exit Sub
        End If
    
        txtVAT.Text = (UnitPrice / 100 * VATrate).ToString("£0.00")
        txtTotal.Text = (UnitPrice + UnitPrice / 100 * VATrate).ToString("£0.00")
    
        lbVATRate.Text = txtVATRate.Text & "%"
        lbAmountEntered.Text = GetDouble(txtInsertPrice.Text).ToString("£0.00")
        lbVatTotal.Text = txtVAT.Text
        lbTotal.Text = txtTotal.Text
    
        cmdCalculateItems.Enabled = True
    
      End Sub
      Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return 0
      End Function
      Function GetDouble(s As String) As Double
        Dim v As Double = 0.0
        If Double.TryParse(s, v) Then Return v
        Return 0.0
      End Function
      Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
        Close()
      End Sub
      Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
        For Each c As Control In Controls
          If c.GetType() Is GetType(TextBox) Then
            c.Text = Nothing
          End If
        Next
        For Each c As Control In Panel1.Controls
          If c.GetType() Is GetType(Label) Then
            If c.Name.StartsWith("lb") Then c.Text = "0"
          End If
        Next
        For Each c As Control In Panel2.Controls
          If c.GetType() Is GetType(Label) Then
            If c.Name.StartsWith("lb") Then c.Text = "0"
          End If
        Next
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by Gary Simpson Wednesday, January 16, 2019 1:58 PM
    Monday, January 14, 2019 11:22 PM
  • Hi Les,

    Thank you for the Code you have kindly written. It certainly tidy's up my code.

    Thanks again Les.

    Regards

    Gary 


    Gary Simpson

    Wednesday, January 16, 2019 1:58 PM