none
txt box to decimal - simple form issue RRS feed

  • Question

  • Hi,


    Am starting to learn VB. Trying to get a txt button BiWeeklyPay to work as calculation. Cannot get the tax labels to fill out.


    Any ideas?


    Option Strict On

    Public Class Form1

        Const _cdecFICATax As Decimal = 7.45D
        Const _cdecFederalTax As Decimal = 0.22D
        Const _cdecStateTax As Decimal = 0.04D


        Private Sub btnComputeTax_Click(sender As Object, e As EventArgs) Handles btnComputeTax.Click

            Dim decBiWeeklyPay As Decimal
            Dim decFICA As Decimal
            Dim decFederal As Decimal
            Dim decState As Decimal
            Dim decBiWeeklyNetPay As Decimal

            decBiWeeklyPay = 

            decFICA = decBiWeeklyPay * _cdecFICATax
            decFederal = decBiWeeklyPay * _cdecFederalTax
            decState = decBiWeeklyPay * _cdecStateTax
            decBiWeeklyNetPay = decBiWeeklyPay - decFICA - decFederal - decState

            lblFICA.Text = decFICA.ToString("C")
            lblFederal.Text = decFederal.ToString("C")
            lblState.Text = decState.ToString("C")
            lblBiWeeklyNetPay.Text = decBiWeeklyNetPay.ToString("C")

        End Sub


    • Edited by trevorpan Saturday, October 7, 2017 11:26 PM
    Saturday, October 7, 2017 11:26 PM

All replies

  • Am starting to learn VB. Trying to get a txt button BiWeeklyPay to work as calculation. Cannot get the tax labels to fill out.

    Is the pay amount some text that is entered into a text box by the user as the application runs?   If so, then the name of that text box is the critical missing bit.  You need the Text property of that text box.   Use it like this example uses 'value':

    https://msdn.microsoft.com/en-us/library/9zbda557%28v=vs.110%29.aspx

    If the pay amount is coming from some other source you need to indicate what that source is.

    Saturday, October 7, 2017 11:34 PM
  • Hi

    This code (copied from yours) does 'fill out' the textboxes.

    The only change I needed to make was to put a value for decBiWeeklyPay = 100D

    The result would probably disappoint the worker:)

    ption Strict On
    Option Explicit On
    Public Class Form1
        Const _cdecFICATax As Decimal = 7.45D
        Const _cdecFederalTax As Decimal = 0.22D
        Const _cdecStateTax As Decimal = 0.04D
        Private Sub btnComputeTax_Click(sender As Object, e As EventArgs) Handles btnComputeTax.Click
    
            Dim decBiWeeklyPay As Decimal
            Dim decFICA As Decimal
            Dim decFederal As Decimal
            Dim decState As Decimal
            Dim decBiWeeklyNetPay As Decimal
    
            ' does this need another tect box for input?
            decBiWeeklyPay = 100D
    
            decFICA = decBiWeeklyPay * _cdecFICATax
            decFederal = decBiWeeklyPay * _cdecFederalTax
            decState = decBiWeeklyPay * _cdecStateTax
            decBiWeeklyNetPay = decBiWeeklyPay - decFICA - decFederal - decState
    
            lblFICA.Text = decFICA.ToString("C")
            lblFederal.Text = decFederal.ToString("C")
            lblState.Text = decState.ToString("C")
            lblBiWeeklyNetPay.Text = decBiWeeklyNetPay.ToString("C")
    
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, October 7, 2017 11:50 PM
    Saturday, October 7, 2017 11:49 PM
  • Hi Les,

    Thank you for your time... And a lousy $100 would not be cool!

    It's for a class, so not a huge deal. Basically, a user enters a biweekly salary into a textbox.

    Then it computes the tax. The text box is BiWeeklyPay, but I can't figure out how to get the decimal to work so decBiWeeklyPay can be multiplied by the taxes. I get an error like dec to decimal not allowed. 

    Should it be a string?

    Sunday, October 8, 2017 2:12 AM
  • Basically, a user enters a biweekly salary into a textbox.

    What is the name of the textbox that the user enters that amount into?  If you know that name then you can access the Text property of the control to get the user entry.  Then convert that to a decimal number using the example provided above.

    Sunday, October 8, 2017 3:56 AM
  • Hi Trevor

    Here is my attempt. Your code changed as follows:

    1.Added a Function to return a valid Decimal number. Textbox text is a string and needs to be changed to a number.

    2. Added a TextBox called Earning for user input.

    3. Removed Clear and Edit button (just for this example)

    4. Changed the tax rate constant to let the employee earn SOME money.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Const _cdecFICATax As Decimal = 0.0745D
        Const _cdecFederalTax As Decimal = 0.022D
        Const _cdecStateTax As Decimal = 0.04D
        Function GetDec(s As String) As Decimal
            ' Get a valid decimal (or return zero)
            Dim v As Decimal = 0.0D
            If Decimal.TryParse(s, v) Then Return v
            Return 0
        End Function
        Private Sub btnComputeTax_Click(sender As Object, e As EventArgs) Handles btnComputeTax.Click
    
            Dim decBiWeeklyPay As Decimal
            Dim decFICA As Decimal
            Dim decFederal As Decimal
            Dim decState As Decimal
            Dim decBiWeeklyNetPay As Decimal
    
            'Earning is User Input TextBox for BiWeekly Pay
            decBiWeeklyPay = GetDec(Earning.Text)
    
            decFICA = decBiWeeklyPay * _cdecFICATax
            decFederal = decBiWeeklyPay * _cdecFederalTax
            decState = decBiWeeklyPay * _cdecStateTax
            decBiWeeklyNetPay = decBiWeeklyPay - decFICA - decFederal - decState
    
            lblFICA.Text = decFICA.ToString("C")
            lblFederal.Text = decFederal.ToString("C")
            lblState.Text = decState.ToString("C")
            lblBiWeeklyNetPay.Text = decBiWeeklyNetPay.ToString("C")
    
        End Sub
    
    End Class


    Regards Les, Livingston, Scotland

    Sunday, October 8, 2017 12:58 PM
  • Having trouble converting a text property to decimal. Do you need to declare two? one as decimal and one as text?

    Dim decBiWeeklyPay As Decimal

    Dim strBiWeeklyPay as String

    Sunday, October 8, 2017 8:43 PM
  • Hi Acamar,

    Does this example make this overly complicated?

    A dollar amount is added and it needs to turn into a decimal for the calculations.



    • Edited by trevorpan Sunday, October 8, 2017 8:45 PM
    Sunday, October 8, 2017 8:45 PM
  • Hi Les, 

    I tried this and get an error:

    Option Strict On
    Option Explicit On

    Public Class Form1

        Const _cdecFICATax As Decimal = 7.45D
        Const _cdecFederalTax As Decimal = 0.22D
        Const _cdecStateTax As Decimal = 0.04D

        Function GetDec(s As String) As Decimal
            ' Get a valid decimal (or return zero)
            Dim v As Decimal = 0.0D
            If Decimal.TryParse(s, v) Then Return v
            Return 0

        End Function

        Private Sub btnComputeTax_Click(sender As Object, e As EventArgs) Handles btnComputeTax.Click

            Dim BiWeeklyPay As String
            Dim decBiWeeklyPay As Decimal
            Dim decFICA As Decimal
            Dim decFederal As Decimal
            Dim decState As Decimal
            Dim decBiWeeklyNetPay As Decimal

            'Earning is User Input TextBox for BiWeekly Pay
            decBiWeeklyPay = GetDec(BiWeeklyPay.Text)

            decFICA = decBiWeeklyPay * _cdecFICATax
            decFederal = decBiWeeklyPay * _cdecFederalTax
            decState = decBiWeeklyPay * _cdecStateTax
            decBiWeeklyNetPay = decBiWeeklyPay - decFICA - decFederal - decState

            lblFICA.Text = decFICA.ToString("C")
            lblFederal.Text = decFederal.ToString("C")
            lblState.Text = decState.ToString("C")
            lblBiWeeklyNetPay.Text = decBiWeeklyNetPay.ToString("C")

        End Sub

        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Close()
        End Sub

        Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
            txtBiWeeklyPay.Clear()
            lblFICA.Text = ""
            lblFederal.Text = ""
            lblState.Text = ""
            lblBiWeeklyNetPay.Text = ""


        End Sub

        Private Sub txtBiWeeklyPay_TextChanged(sender As Object, e As EventArgs)
            txtBiWeeklyPay.Focus()
        End Sub

    End Class

    Sunday, October 8, 2017 8:48 PM
  • Do you need to declare two? one as decimal and one as text?

    No.    You simply need to convert the Text property of the text box into the numeric value in decBiWeeklyPay.

    Use the same code as this example uses to convert the string 'value' into the numeric value in 'number'.
    https://msdn.microsoft.com/en-us/library/9zbda557%28v=vs.110%29.aspx

    • Edited by Acamar Sunday, October 8, 2017 8:59 PM sp
    Sunday, October 8, 2017 8:57 PM
  • Does this example make this overly complicated?

    Yes.   You are not turning a dollar amount into a decimal.  You are turning a string (the Text property of the textbox control) into a decimal and you are reporting an error to the user if that conversion did not work.  It's 5 lines of code.

    Sunday, October 8, 2017 9:03 PM