我需要完成的程序专注于 try catch 块。我遇到的问题是每次用户尝试输入任何内容时都会显示错误消息,甚至是有效的数字数据。过去几个小时我一直在试图弄清楚,也许有人能看到我的错误......
Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
'Convert input values to numeric variables.
Dim SnowboardInteger, WithBootsInteger, TotalInteger As Integer
Try
'Convert snowboard to numeric variable.
SnowboardInteger = Integer.Parse(SnowboardsTextBox.Text)
Try
'Convert with boots if snowboard was sucessful.
WithBootsInteger = Integer.Parse(WithBootsPriceTextBox.Text)
'Calculate the total price.
TotalInteger = SnowboardInteger + WithBootsInteger
'Calculate summary values.
SnowboardSumInteger += SnowboardInteger
WithBootsSumInteger += WithBootsInteger
TotalSumInteger += TotalSumInteger
SaleCountInteger += 1
AverageInteger = TotalSumInteger / SaleCountInteger
'Format and display price values.
SnowboardsPriceTextBox.Text = SnowboardInteger.ToString("c")
WithBootsPriceTextBox.Text = WithBootsInteger.ToString("c")
TotalPriceTextBox.Text = TotalInteger.ToString("c")
'Format and display summary values.
SnowboardRentalTextBox.Text = SnowboardSumInteger.ToString()
WithBootsRentalTextBox.Text = WithBootsSumInteger.ToString()
TotalChargesTextBox.Text = TotalSumInteger.ToString()
AverageChargeTextBox.Text = AverageInteger.ToString()
Catch Snowboards As FormatException
'Handle a snowboard exception.
MessageBox.Show("Snowboards value must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
With SnowboardsTextBox
.Focus()
.SelectAll()
End With
End Try
Catch WithBoots As FormatException
'Handle a with boots exception.
MessageBox.Show("Snowboards with Boots value must be numeric.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
With WithBootsTextBox
.Focus()
.SelectAll()
End With
Catch AnException As Exception
'Handle any other exceptions.
MessageBox.Show("Error: " & AnException.Message)
End Try
End Sub