2

我这个学期正在学习 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
4

4 回答 4

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

您可能希望在其他地方执行这些计算,因此能够从点击事件之外的其他地方调用它们会很好。 DataComplete可能小到:

Return NameTextBox.Text.Length > 0

稍后可能需要查看是否存在这样的销售人员等

于 2013-09-18T01:58:20.930 回答
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()方法来测试文本框是否有值。我发现这种语法比检查空字符串更干净。

于 2013-09-18T02:43:04.750 回答
0

你快到了。只需稍微按摩一下您的代码,它就可以正常工作。

If NameTextBox.Text = "" Then
   MessageBox.Show(...)
Else
   Do Calculation...
End If
于 2013-09-18T02:11:56.753 回答
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
于 2016-04-08T13:57:18.967 回答