我正在为学校制作一个视觉基础项目,用于计算住院期间的总数。它在大多数情况下都有效,但是当输入负数并且我没有收到该消息时,我应该会收到错误消息。这是我现在的编码:
Public Class Form1
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'close form
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'clear form
txtLab.Clear()
txtMedication.Clear()
txtPhysical.Clear()
txtStay.Clear()
txtSurgical.Clear()
txtStay.Focus()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'class level declaration
Const DayRate As Decimal = 350D 'cost per day
'declare variables
Dim Physical As Decimal
Dim Surgical As Decimal
Dim Lab As Decimal
Dim Medication As Decimal
Dim Stay As Decimal
Try
'Copy the scores into the variables
Stay = CDec(txtStay.Text) * DayRate
Medication = CDec(txtMedication.Text)
Surgical = CDec(txtSurgical.Text)
Lab = CDec(txtLab.Text)
Physical = CDec(txtPhysical.Text)
Catch
MessageBox.Show("Please enter numeric values")
Return
'Find out if box enteries are negative or not
If Convert.ToDecimal(Lab) < 0 Then
MessageBox.Show("No Negative Numbers", "Try Agian")
Return
txtLab.SelectAll()
ElseIf Convert.ToDecimal(Medication) < 0 Then
MessageBox.Show("No Negative Numbers", "Try Agian")
Return
txtMedication.SelectAll()
ElseIf Convert.ToDecimal(Surgical) < 0 Then
MessageBox.Show("No Negative Numbers", "Try Agian")
Return
txtSurgical.SelectAll()
ElseIf Convert.ToDecimal(Physical) < 0 Then
MessageBox.Show("No Negative Numbers", "Try Agian")
Return
txtPhysical.SelectAll()
ElseIf Convert.ToDecimal(Stay) < 0 Then
MessageBox.Show("No Negative Numbers", "Try Agian")
Return
txtStay.SelectAll()
End If
End Try
End Sub
Function CalcStayCharges() As Decimal
CalcStayCharges = (CDec(txtStay.Text) * 350)
End Function
Function CalcMiscCharges() As Decimal
CalcMiscCharges = CDec(txtMedication.Text) + CDec(txtLab.Text) + CDec(txtPhysical.Text) + CDec(txtSurgical.Text)
End Function
Function CalctotalCharges() As Decimal
CalctotalCharges = (CalcStayCharges() + CalcMiscCharges())
End Function
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'calculates total of stay
Dim decTotal As Decimal
decTotal = CalctotalCharges()
lblTotal.Text = decTotal.ToString("c")
End Sub
End Class
答案 0 :(得分:1)
Catch 子句中的代码不会执行,因为不会发生异常。您应该在 Catch 子句之前移动 If .. Else .. 语句。