Question

I have an assignment I have been working on in VB. I have attached the assignment below:

(I would've attached the image but I cannot because I am a new user)

Assignment Question

(http://i.imgur.com/LPZre2F.jpg)

I have written all of the code but for some reason I cannot get the calculate button to calculate anything. Nor, can I get the messagebox to show in the Calculate button when there is no input in the NameTextBox or the PieceTextBox.

Can someone spot something that I am missing on why this may not be working?

Public Class Form1
'Declare Variables
Dim Pieces As Integer
Dim AmtEar As Decimal
Dim NoPieces As Decimal = 0
Dim totPay As Decimal = 0
Dim avgPay As Decimal = 0.0
'Declare Confirm Message Variable
Dim confirmOpt As DialogResult


Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Try
        'Declare constant rates paid out
        Const priceRate1 As Decimal = 0.5
        Const priceRate2 As Decimal = 0.55
        Const priceRate3 As Decimal = 0.6
        Const priceRate4 As Decimal = 0.65

        'Check the blank fields (name and number of pieces)
        If NameTextBox.Text = "" Then
            MessageBox.Show("Please Enter Name:")
        Else
            If PieceTextBox.Text = "" Then
                MessageBox.Show("Please Enter Number of Pieces Produced")
            Else
                'Count number of pieces
                NoPieces += 1
                'Read number of pieces
                Pieces = Integer.Parse(PieceTextBox.Text)

                'Use select case for given constraints
                'calculate earned amount for each
                Select Case Pieces
                    Case 1 To 199
                        AmtEar = priceRate1 * Pieces
                        totPay += AmtEar
                    Case 200 To 399
                        AmtEar = priceRate2 * Pieces
                        totPay += AmtEar
                    Case 400 To 599
                        AmtEar = priceRate3 * Pieces
                        totPay += AmtEar
                    Case Is >= 600
                        AmtEar = priceRate4 * Pieces
                        totPay += AmtEar
                    Case Else
                        MessageBox.Show("Number of Pieces Produced Must Be Numeric")
                End Select

                'Display amount earned
                AmtTextBox.Text = AmtEar.ToString("C")
            End If

        End If
        'Exception for wrong input
    Catch ex As FormatException
        MessageBox.Show("Enter Valid Data", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try

End Sub

Private Sub SummaryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryButton.Click
    'calculates summary total
    If (NoPieces >= 1) Then
        'calculate and update total pieces, total pay and average pay
        avgPay = totPay / NoPieces
        TotalProducedTextBox.Text = NoPieces.ToString()
        TotlPayTextBox.Text = NoPieces.ToString("C")
        AveragePayTextBox.Text = avgPay.ToString("C")
    Else
        'Otherwise display message
        MessageBox.Show("Enter at Least One Employee Data", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub

Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
    'Clear name and number of pieces
    NameTextBox.Clear()
    PieceTextBox.Clear()
    AmtTextBox.Clear()
End Sub

Private Sub ClearAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearAllButton.Click
    'If no data is available
    If NoPieces = 0 Then
        MessageBox.Show("No Data Available To Delete")
    Else
        'Confirm option message
        confirmOpt = MessageBox.Show("Are You Sure You Want To Delete Summary", "Confirm", MessageBoxButtons.YesNo)
        'Check if yes then clear all inputs and totals
        If confirmOpt = DialogResult.Yes Then
            avgPay = 0
            totPay = 0
            NoPieces = 0
            NameTextBox.Clear()
            PieceTextBox.Clear()
            AmtTextBox.Clear()
            TotalProducedTextBox.Clear()
            TotlPayTextBox.Clear()
            AveragePayTextBox.Clear()
        End If

    End If

    End Sub
End Class
Was it helpful?

Solution

You are missing the Click-handle for that button.
Change this:

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

to something like this:

Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top