我这个学期正在学习VB,虽然这个特定的作业并不需要,但我很好奇如果文本框留空,我怎么能创建一个MessageBox错误。
我们刚刚介绍了使用Try / Catch语句,并且我已成功为文本仅为数字的文本框创建了MessageBoxes。例如,用户必须输入名称和两个数字金额。如果名称文本框保留为空,我试图创建一个错误,停止程序计算两个金额并将插入点返回到名称文本框。
有更简单的方法吗?我目前拥有的If / Then语句并没有阻止程序计算(我刚刚开始,所以对我很轻松):
Private Sub CalculateButton_Click(sender As System.Object, e As System.EventArgs) Handles CalculateButton.Click
'Declare Variables
Dim SellingPrice, CostValue, Commission As Decimal
'Test to see if a name was provided the NameTextBox
If NameTextBox.Text = "" Then
MessageBox.Show("Please enter a Salesperson's name", "No entry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With NameTextBox
.Focus()
.SelectAll()
End With
End If
'Test if Numerical data was entered for SellingPriceTextBox
Try
'Convert Selling Price
SellingPrice = Decimal.Parse(SellingPriceTextBox.Text)
'Test if Numerical data was entered for CostValueTextBox
Try
'Convert Cost Value
CostValue = Decimal.Parse(CostValueTextBox.Text)
'Calculate the Commission earned
Commission = Decimal.Round(COMMISSION_RATE * (SellingPrice - CostValue), 2)
'Format and display results
TotalCommissionLabel.Text = Commission.ToString("C")
Catch CostValueException As FormatException
'Handle a Cost Value exception
MessageBox.Show("Value must be a numeric value.", "Invalid Input",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
With CostValueTextBox
.Focus()
.SelectAll()
End With
End Try
Catch SellingPriceException As FormatException
'Handle a Selling Price exception
MessageBox.Show("Price must be a numeric value.", "Invalid Input",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
With SellingPriceTextBox
.Focus()
.SelectAll()
End With
End Try
End Sub
答案 0 :(得分:2)
If NameTextBox.Text = "" Then
MessageBox.Show("Please enter a Salesperson's name", "No entry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With NameTextBox
.Focus()
.SelectAll()
End With
Exit Sub ' tell it to skip the rest
End If
我将该部分拆分为一个函数来确定数据是否有效,并且仅在计算有效时才调用它。类似的东西:
Private Sub CalculateButton_Click...
If DataComplete() Then
DoCalcs()
End If
您可能希望在其他地方执行这些计算,因此能够从click事件以外的地方调用它们会很好。 DataComplete
可能只有:
Return NameTextBox.Text.Length > 0
稍后可能需要查看这样的销售人员是否存在等等
答案 1 :(得分:0)
你快到了。只需对代码进行一点按摩即可使其正常工作。
If NameTextBox.Text = "" Then
MessageBox.Show(...)
Else
Do Calculation...
End If
答案 2 :(得分:0)
一种方法是使用If-Else
逻辑,如下所示:
Private Sub CalculateButton_Click(sender As System.Object, e As System.EventArgs) Handles CalculateButton.Click
'Test to see if a name was provided the NameTextBox
If String.IsNullOrEmpty(NameTextBox.Text) Then
MessageBox.Show("Please enter a Salesperson's name", "No entry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
With NameTextBox
.Focus()
.SelectAll()
End With
Else
'Declare Variables
Dim SellingPrice, CostValue, Commission As Decimal
'Test if Numerical data was entered for SellingPriceTextBox
Try
'Convert Selling Price
SellingPrice = Decimal.Parse(SellingPriceTextBox.Text)
'Test if Numerical data was entered for CostValueTextBox
Try
'Convert Cost Value
CostValue = Decimal.Parse(CostValueTextBox.Text)
'Calculate the Commission earned
Commission = Decimal.Round(COMMISSION_RATE * (SellingPrice - CostValue), 2)
'Format and display results
TotalCommissionLabel.Text = Commission.ToString("C")
Catch CostValueException As FormatException
'Handle a Cost Value exception
MessageBox.Show("Value must be a numeric value.", "Invalid Input",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
With CostValueTextBox
.Focus()
.SelectAll()
End With
End Try
Catch SellingPriceException As FormatException
'Handle a Selling Price exception
MessageBox.Show("Price must be a numeric value.", "Invalid Input",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
With SellingPriceTextBox
.Focus()
.SelectAll()
End With
End Try
End If
End Sub
这可以保证向用户显示错误消息,指示他们需要输入销售人员的姓名或执行计算逻辑,但不能同时执行两者。
注意:我还添加了使用String.IsNullOrEmpty()
方法来测试文本框是否有值。我发现语法比检查空字符串更清晰。
答案 3 :(得分:0)
If lblPriceTotalClothes.Text & lblPriceTotalElectronics.Text & lblPriceTotalGames.Text & lblPriceTotalJewellry.Text = Nothing Then
MessageBox.Show("Please choose something to buy!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
lblSubtotalPrice.Text = FormatCurrency(0)
lblTotalCostPrice.Text = FormatCurrency(0)
lbltaxesPrice.Text = FormatCurrency(0)
Return
Else
lblSubtotalPrice.Text = FormatCurrency(subTotalPrice)
taxes = FormatCurrency(subTotalPrice * 0.13)
lblTotalCostPrice.Text = FormatCurrency(subTotalPrice + taxes)
lbltaxesPrice.Text = FormatCurrency(taxes)
MessageBox.Show("Thank you for shopping!", "Thank You", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If