2014-03-30 71 views
1

我有一個任務,我一直在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 

回答

0

您缺少該按鈕的點擊控制柄。
更改此:

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