Question "0"s in the text

Graciemygirl

New member
Joined
Apr 14, 2013
Messages
1
Programming Experience
1-3
Just learning VB and I got this far...all functions work but a requirement is to have "0"s in the text and Label boxes upon clearing the form...I cannot get it to work correctly, any help would be appreciated!
Public Class Form1


    Private Sub btnCalculateTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateTotal.Click

        'Declare Varibles for calculations
        Dim decCalculatedTotalTax As Decimal            'Total Tax Owed
        Dim decCountyTax1 As Decimal                    'County Tax Owed
        Dim decStateSalesTax1 As Decimal                'Total Tax Owed
        Const decCountyTax_Rate As Decimal = 0.02D      'County Tax Rate
        Const decStateSalesTax_Rate As Decimal = 0.04D  'State Tax Rate

        Try
            'Calculate County Tax and Display
            decCountyTax1 = CDec(txtTotalSales.Text) *
                            decCountyTax_Rate
            lblCountyTax1.Text = decCountyTax1.ToString("c")

            'Calculate State Tax and Display
            decStateSalesTax1 = CDec(txtTotalSales.Text) *
                                decStateSalesTax_Rate
            lblStateSalesTax1.Text = decStateSalesTax1.ToString("c")

            'Calculate Total Tax Owed
            decCalculatedTotalTax = decStateSalesTax1 + decCountyTax1
            lblCalculatedTotalTax.Text = decCalculatedTotalTax.ToString("c")

        Catch
            'Error Message
            MessageBox.Show("Correct Total Sales Amount...Must Be A Dollar Amount.")
        End Try
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clear all fields on the form and set the cursor back at the top
        lblCountyTax1.Text = String.Empty

        'Alternate technique to clear
        txtTotalSales.Clear()
        lblCountyTax1.Text = String.Empty

        lblStateSalesTax1.Text = String.Empty
        lblCalculatedTotalTax.Text = String.Empty

        'Sets Cursor Back at top in Enter Total Sales
        txtTotalSales.Focus()


    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Close the Form
        Me.Close()
    End Sub
End Class
 
Last edited by a moderator:
If you want a display of "0", then why not just do this for all the textboxes and labels: For example:

txtSales.Text = "0"
lblTotal.Text = "0"
 
Graciemygirl, I have to give you props for being one of the few on here to actually post the code you have when asking a question & you're doing type conversions on top of it too, I'm impressed.
Also, I've edited your post to wrap the code in the forums xcode tag, as you can see it's far easier to read on here now.

Solitaire has posted the most basic way to set a textbox or label to "0" instead of setting it to an empty string (String.Empty or calling it's Clear() property). Though if you want get into formatting it as currency you could make a couple of properties on your form and use the Set part to not only assign a value but also assign it as a formatted string, like currency or something. Here's an example:
'Created a private property to get/set the currency value from the Label on the form
Private Property CountyTax1() As Decimal
    Get
        Dim val As Decimal

        'Try to get the input from the user
        If Decimal.TryParse(lblCountyTax1.Text.Trim, val)
            Return Val
        Else
            Return 0D
        End If
    End Get
    Set (value As Decimal)
        lblCountyTax1.Text = value.ToString("c") 'Format it as currency
    End Set
End Property

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    'Clear all fields on the form and set the cursor back at the top
    Me.CountyTax1 = 0D
 
    'Alternate technique to clear
    txtTotalSales.Clear()
    lblCountyTax1.Text = String.Empty

    lblStateSalesTax1.Text = String.Empty
    lblCalculatedTotalTax.Text = String.Empty
 
    'Sets Cursor Back at top in Enter Total Sales
    txtTotalSales.Focus()
End Sub
 
Back
Top