Hello again! I'm almost finished this project. However, the Clear button stopped doing its thing. It keeps giving me an "Invalid Cast Exception" whenever I am pressing the clear button. The project includes 2 forms--- JobInformation and Summary.

The code follows:

'Program Name:      JobInformation
'Programmer:        Zabinga
'Date:              11/28/2011
'Description:       This program produces a summary of the amounts due for a bill for Pat's Auto Repair Shop.
'                   It incorporates menus, job information, a splash form, a summary form, and an About box form.
'Form:              JobInformation

Option Strict On

Public Class JobInformation
    'Declare projectwide variables.

    Public Shared JobNumberString As String
    Public Shared CustomerNameString As String
    Public Shared PartsDecimal As Decimal
    Public Shared HoursOfLaborDecimal As Decimal
    
    'Declare module-level variables.
    Public Shared SubTotalDecimal As Decimal
    Public Shared SalesTaxRateDecimal As Decimal
    Public Shared TotalDecimal As Decimal
    Public Shared LaborInteger As Integer


    'Declare constants.
    Const SALES_TAX_RATE_Decimal As Decimal = 0.08D
    Const LABOR_COST_Decimal As Decimal = 50D

    'Private Sub CustomerNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerNameTextBox.TextChanged
    'CustomerNameString = CustomerNameTextBox.Text
    'End Sub

    Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

        ' Calculate and display the current amounts and add to totals.
        ' Declare on the DIM level.

        ' Declare a WithEvents variable.



        Dim PartsDecimal As Decimal
        Dim SubTotalDecimal As Decimal
        Dim SalesTaxRateDecimal As Decimal
        Dim TotalDecimal As Decimal
        Dim HoursOfLaborDecimal As Decimal
        'Dim CustomerNameString As String


        'Calculate the extended price and add to job total.
        Try
            PartsDecimal = Decimal.Parse(PartsTextBox.Text)
            HoursOfLaborDecimal = Decimal.Parse(HoursOfLaborTextBox.Text)
            SubTotalDecimal = PartsDecimal + LABOR_COST_Decimal * HoursOfLaborDecimal
            TotalDecimal = SubTotalDecimal + (SALES_TAX_RATE_Decimal * SubTotalDecimal)
            If PartsDecimal > 0 Then
                'Call a function procedure.
                SalesTaxRateDecimal = FindTax(SubTotalDecimal)

            ElseIf HoursOfLaborDecimal > 0 Then

                HoursOfLaborDecimal = LABOR_COST_Decimal * HoursOfLaborDecimal


            Else
                SalesTaxRateDecimal = 0
            End If

            TotalDecimal = SubTotalDecimal + (SALES_TAX_RATE_Decimal * SubTotalDecimal)
            HoursOfLaborTextBox.Text = HoursOfLaborDecimal.ToString("N")
            PartsTextBox.Text = PartsDecimal.ToString("C")
            SubTotalTextBox.Text = SubTotalDecimal.ToString("N")
            SalesTaxRateTextBox.Text = SALES_TAX_RATE_Decimal.ToString("N")
            TotalTextBox.Text = TotalDecimal.ToString("C")
            CustomerNameTextBox.Text = CustomerNameString.ToString()

            'Allow clear for new job only.
            ClearButton.Enabled = True

        Catch PartsException As FormatException
            MessageBox.Show("Quantity must be numeric.", "Data entry error",
                            MessageBoxButtons.OK, MessageBoxIcon.Information)
            With PartsTextBox
                .Focus()
                .SelectAll()
            End With

        End Try

    End Sub
    Private Function FindTax(ByVal PartsDecimal As Decimal) As Decimal
        'Calculate the sales tax.

        Return (PartsDecimal * SALES_TAX_RATE_Decimal) + PartsDecimal

    End Function


    Private Sub JobNumberTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JobNumberTextBox.TextChanged
        JobNumberString = JobNumberTextBox.Text
    End Sub

    Private Sub CustomerNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerNameTextBox.TextChanged

        CustomerNameString = CustomerNameTextBox.Text

    End Sub


    Private Sub PartsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PartsTextBox.TextChanged
        PartsDecimal = CDec(PartsTextBox.Text)
    End Sub


    Private Sub HoursOfLaborTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HoursOfLaborTextBox.TextChanged
        HoursOfLaborDecimal = CDec(HoursOfLaborTextBox.Text)
    End Sub
    Private Sub SubTotalTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubTotalTextBox.TextChanged
        SubTotalDecimal = CDec(SubTotalTextBox.Text)
    End Sub
    Private Sub SalesTaxRateTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalesTaxRateTextBox.TextChanged
        SalesTaxRateDecimal = CDec(SalesTaxRateTextBox.Text)
    End Sub
    Private Sub TotalTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTextBox.TextChanged
        TotalDecimal = CDec(TotalTextBox.Text)

    End Sub



    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click


        'Clear the appropriate textboxes.

        JobNumberTextBox.Clear()
        CustomerNameTextBox.Clear()
        PartsTextBox.Clear()
        HoursOfLaborTextBox.Clear()
        SubTotalTextBox.Clear()
        SalesTaxRateTextBox.Clear()
        TotalTextBox.Clear()
        With JobNumberTextBox
            .Clear()
            .Focus()
            .SelectAll()

        End With

        PartsDecimal = 0
        HoursOfLaborDecimal = 0
        SubTotalDecimal = 0
        SalesTaxDecimal = 0
        TotalDecimal = 0



    End Sub


    Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click
        Me.Close()
    End Sub
End Class

Recommended Answers

All 21 Replies

Yes, because you clear your textboxes, which raise the "TextChanged" Events.
Just add a check to your "TextChanged" Events for Text being empty. this will solve your problem.

I don't understand what you mean by 'check'

I changed the summary textboxes to labels, and everything works at that point. Except the clear button, when depressed, runs into an error -- InvalidCastExeption was unhandled.

also it follows--- Conversion from string "" to type 'Decimal' is not valid.

ok....

TotalTextBox.Clear()

is the same as TotalTextBox.Text = ""
This cause the TextChanged event:

Private Sub TotalTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTextBox.TextChanged
        TotalDecimal = CDec(TotalTextBox.Text) 'on clearing the textbox you try to cast an empty string to decimal!
End Sub

Woah, I forgot about the "" as being a blank string (I could be wrong or said it wrong). I ran the program and it runs fine, along with the summary form. Many thanks! My instructor helped me with that clear button problem, but he told me to just comment the entire textboxes cos he thought it was redundant. So I did just that, but then my summary labels started to show nothing! so it took me two hours to try to solve it and it drove me nuts! Simple methods like the one you showed me can save tons of time. Thanks again!

actually, when I checked the program again, the clear button got the same error again.

Change

Private Sub TotalTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTextBox.TextChanged
        TotalDecimal = CDec(TotalTextBox.Text) 
End Sub

to:

Private Sub TotalTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTextBox.TextChanged
dim dummy as decimal
if not Decimal.tryparse(TotalTextBox.Text, dummy) then return ' this will do nothing if the textbox is empty OR contains any text that can not be converted to decimal
        TotalDecimal = dummy 
End Sub

Of course you h ave to add this check to all of your textboxes textchanged events.

Can you please explain what you call the last block of code and what it does? thanks.

The clear textboxes now work, but now my summary doesn't work. I input the information in the textboxes, then i calculate. Everything's fine then, but when I choose the Summary menu from the drop down box, it displays 4 things but it excludes the subtotal, sales tax, and total. I tried to debug it, but I couldn't solve it, even though it worked fine before (should never break things when they are fine). I was tinkering around and I messed it up.

I have code for both forms.

'Program Name:      JobInformation
'Programmer:        Zabinga
'Date:              11/28/2011
'Description:       This program produces a summary of the amounts due for a bill for Pat's Auto Repair Shop.
'                   It incorporates menus, job information, a splash form, a summary form, and an About box form.
'Form:              JobInformation

Option Strict On

Public Class JobInformation
    'Declare projectwide variables.

    Public Shared JobNumberString As String
    Public Shared CustomerNameString As String
    Public Shared PartsDecimal As Decimal
    Public Shared HoursOfLaborDecimal As Decimal
    
    'Declare module-level variables.
    Public Shared SubTotalDecimal As Decimal
    Public Shared SalesTaxRateDecimal As Decimal
    Public Shared TotalDecimal As Decimal
    Public Shared LaborInteger As Integer


    'Declare constants.
    Const SALES_TAX_RATE_Decimal As Decimal = 0.08D
    Const LABOR_COST_Decimal As Decimal = 50D

    

    Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click

        ' Calculate and display the current amounts and add to totals.

        ' Declare on the DIM level.

        Dim PartsDecimal As Decimal
        Dim SubTotalDecimal As Decimal
        Dim SalesTaxRateDecimal As Decimal
        Dim TotalDecimal As Decimal
        Dim HoursOfLaborDecimal As Decimal



        'Calculate the extended price and add to job total.
        Try
            PartsDecimal = Decimal.Parse(PartsTextBox.Text)
            HoursOfLaborDecimal = Decimal.Parse(HoursOfLaborTextBox.Text)
            SubTotalDecimal = PartsDecimal + LABOR_COST_Decimal * HoursOfLaborDecimal
            TotalDecimal = SubTotalDecimal + (SALES_TAX_RATE_Decimal * SubTotalDecimal)
            If PartsDecimal > 0 Then
                'Call a function procedure.
                SalesTaxRateDecimal = FindTax(SubTotalDecimal)

            ElseIf HoursOfLaborDecimal > 0 Then

                HoursOfLaborDecimal = LABOR_COST_Decimal * HoursOfLaborDecimal


            Else
                SalesTaxRateDecimal = 0
                PartsDecimal = 0
                HoursOfLaborDecimal = 0
            End If

            TotalDecimal = SubTotalDecimal + (SALES_TAX_RATE_Decimal * SubTotalDecimal)
            HoursOfLaborTextBox.Text = HoursOfLaborDecimal.ToString("N")
            PartsTextBox.Text = PartsDecimal.ToString("C")
            SubTotalTextBox.Text = SubTotalDecimal.ToString("C")
            SalesTaxRateTextBox.Text = SALES_TAX_RATE_Decimal.ToString("C")
            TotalTextBox.Text = TotalDecimal.ToString("C")


            'Allow clear for new job only.
            ClearButton.Enabled = True

        Catch PartsException As FormatException



            'Handle a price exception.

            MessageBox.Show("Must be numeric.", "Data entry error",
                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            With PartsTextBox
                .Focus()
                .SelectAll()

            End With


        End Try

    End Sub
    Private Function FindTax(ByVal PartsDecimal As Decimal) As Decimal
        'Calculate the sales tax.

        Return (PartsDecimal * SALES_TAX_RATE_Decimal) + PartsDecimal

    End Function

    'Property procedures for read-only.

    ReadOnly Property CustomerName() As String
        Get
            Return CustomerNameString

        End Get
    End Property


    ReadOnly Property Parts() As Decimal
        Get
            Return PartsDecimal
        End Get
    End Property


    ReadOnly Property SubTotal() As Decimal
        Get
            Return SubTotalDecimal
        End Get
    End Property

    ReadOnly Property SalesTaxRate() As Decimal
        Get
            Return SalesTaxRateDecimal

        End Get
    End Property

    ReadOnly Property Total() As Decimal
        Get
            Return TotalDecimal

        End Get
    End Property

    ReadOnly Property HoursOfLabor() As Decimal
        Get
            Return HoursOfLaborDecimal
        End Get
    End Property

    Private Sub JobNumberTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JobNumberTextBox.TextChanged
        JobNumberString = JobNumberTextBox.Text
    End Sub

    Private Sub CustomerNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerNameTextBox.TextChanged

        CustomerNameString = CustomerNameTextBox.Text

    End Sub


    Private Sub PartsTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PartsTextBox.TextChanged
        Dim audi As Decimal
        If Not Decimal.TryParse(PartsTextBox.Text, audi) Then Return ' this will do nothing if the textbox is empty or contains any text cannot be converted to decimal
        PartsDecimal = audi
    End Sub


    Private Sub HoursOfLaborTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HoursOfLaborTextBox.TextChanged
        Dim audi As Decimal
        If Not Decimal.TryParse(HoursOfLaborTextBox.Text, audi) Then Return ' this will do nothing if the textbox is empty or contains any text cannot be converted to decimal
        HoursOfLaborDecimal = audi
    End Sub
    Private Sub SubTotalTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubTotalTextBox.TextChanged
        Dim audi As Decimal
        If Not Decimal.TryParse(SubTotalTextBox.Text, audi) Then Return ' this will do nothing if the textbox is empty or contains any text that cannot be converted to decimal
        SubTotalDecimal = audi
    End Sub
    Private Sub SalesTaxRateTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalesTaxRateTextBox.TextChanged
        Dim audi As Decimal
        If Not Decimal.TryParse(SalesTaxRateTextBox.Text, audi) Then Return ' this will do nothing if the textbox is empty or contains any text cannot be converted to decimal
        SalesTaxRateDecimal = audi
    End Sub
    Private Sub TotalTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotalTextBox.TextChanged
        Dim audi As Decimal
        If Not Decimal.TryParse(TotalTextBox.Text, audi) Then Return ' this will do nothing if the textbox is empty OR contains any text that can not be converted to decimal
        TotalDecimal = audi
    End Sub
    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click



        'Clear the appropriate textboxes.

        JobNumberTextBox.Text = ""
        CustomerNameTextBox.Text = ""
        PartsTextBox.Text = ""
        HoursOfLaborTextBox.Text = ""
        SubTotalTextBox.Text = ""
        SalesTaxRateTextBox.Text = ""
        TotalTextBox.Text = ""
        With JobNumberTextBox
            .Clear()
            .Focus()

        End With

        PartsDecimal = 0
        HoursOfLaborDecimal = 0
        SubTotalDecimal = 0
        SalesTaxDecimal = 0
        TotalDecimal = 0



    End Sub


    Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click
        Me.Close()
    End Sub
End Class
'Program name:   Summary of JobInformation to Pat's Auto Repair Shop.
'Programmer:    Zabinga
'Date:          12/2/2011
'Description:   This program retrieves the summary calculations from properties of the main form.

Option Strict On

Public Class Summary


    Inherits System.Windows.Forms.Form
    'Inheritance statement.

    Class NewClass
        Inherits JobInformation
    End Class
    'Declare a With Events variable.

    'Dim WithEvents Summary As New JobInformation



    'The property procedure for a read-only property.
    ReadOnly Property Summary() As JobInformation
        Get
            Return JobInformation
        End Get
    End Property


    Dim CustomerNameTextBoxString As String
    Dim SalesTaxRateTextBoxDecimal As Decimal
    Dim PartsTextBoxDecimal As Decimal
    Dim HoursOfLaborTextBoxDecimal As Decimal
    Dim JobNumberTextBoxString As String
    Dim SubTotalTextBoxDecimal As Decimal
    Dim TotalTextBoxDecimal As Decimal




    Private Sub Summary_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
        'Get the data from the properties of JobInformation form. Converting data to strings.

        JobNumberLabel.Text = JobInformation.JobNumberString.ToString()
        CustomerNameLabel.Text = JobInformation.CustomerNameString.ToString()
        LaborLabel.Text = JobInformation.HoursOfLaborDecimal.ToString("N")
        PartsLabel.Text = JobInformation.PartsDecimal.ToString("C")
        SubTotalLabel.Text = JobInformation.SubTotalDecimal.ToString("C")
        SalesTaxRateLabel.Text = JobInformation.SalesTaxRateDecimal.ToString("C")
        TotalLabel.Text = JobInformation.TotalDecimal.ToString("C")

    End Sub


    Private Sub Summary_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Populate the labels, define the property at the module level.


        JobNumberLabel.Text = Summary.JobNumberTextBox.Text
        CustomerNameLabel.Text = Summary.CustomerNameTextBox.Text
        LaborLabel.Text = Summary.HoursOfLaborTextBox.Text
        PartsLabel.Text = Summary.PartsTextBox.Text
        SubTotalLabel.Text = Summary.SubTotalTextBox.Text
        SalesTaxRateLabel.Text = Summary.SalesTaxRateTextBox.Text
        TotalLabel.Text = Summary.TotalTextBox.Text

    End Sub



    Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click
        'Exit the project.

        Me.Close()
    End Sub


End Class

Where is the event from the dropdownxox?

that would be the main form:

Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        'Show the MessageBox for About.
        AboutBox1.ShowDialog()

End Sub


Private Sub JobInformationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles JobInformationToolStripMenuItem.Click
        'Show the JobInformation Form.
        JobInformation.ShowDialog()
    End Sub
    Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click
        'Show the Summary form.
        Summary.ShowDialog()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'Exit the program.
        Me.Close()
    End Sub
End Class

Set a breakpoint at JobNumberLabel.Text = Summary.JobNumberTextBox.Text and see if Summary is nothing (which i would expect)

i didn't see any reading from the break.

Would you mind to zip your solution and attach it on this thread. so i can take a look on it. Hope you use VS2010 though.

ok, and yes i am using VB2010.

i've attached the project.

I cant see the attachment. Are you sure you attached it correctly?

here it is.

right, found your mistake...

remove these lines from your CalculateButton_Click event.
Else you are not assigning the values to the shared variables, only to the local ones.

Dim PartsDecimal As Decimal
        Dim SubTotalDecimal As Decimal
        Dim SalesTaxRateDecimal As Decimal
        Dim TotalDecimal As Decimal
        Dim HoursOfLaborDecimal As Decimal

one problem I found. when you press the calculate button, you have everything correct. but when you go to the summary page, the sales tax label is different-- it displays the total.

ignore my previous statement. I fixed whatever needed to be fixed, but I forget how I did it. But as long as it's fixed, I guess I am happy. Thanks a ton!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.