不计算或显示MessageBox

时间:2014-03-30 18:50:25

标签: vb.net vb.net-2010

我有一个我一直在VB工作的作业。我已附上以下作业:

(我会附上图片,但我不能,因为我是新用户)

作业问题

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

我已编写了所有代码但由于某种原因我无法获得计算按钮来计算任何内容。当NameTextBox或PieceTextBox中没有输入时,我是否可以在“计算”按钮中显示消息框。

有人能发现一些我错过的东西,为什么这可能不起作用?

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

1 个答案:

答案 0 :(得分:0)

您缺少该按钮的Click-handle 改变这个:

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

这样的事情:

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