我不知道如何正确放置“ IF语句”的代码块

时间:2018-11-21 07:08:14

标签: vb.net

我想要做的就是正确执行If语句代码块。据说这个想法很简单。 在数量部分:  如果不是数字,则返回错误 否则,如果它是一个大于0的数字,则“接受”消息框 否则,如果数字为零,则消息框“数量为0” 否则,如果数字小于零,则出现消息框“错误:数量小于零”

但是,当我尝试运行该程序时,当我尝试输入等于RequestQuantity3TxtBx或小于0的数字时,它什么也没做。

If IsNumeric(RequestQuantityTxtBx.Text) Then
    num1 = RequestQuantity2TxtBx.Text
    num2 = RequestQuantityTxtBx.Text
    total = num1 - num2
    RequestQuantity3TxtBx.Text = total

    If RequestQuantity3TxtBx.Text > 0 Then

        If RequestQuantity3TxtBx.Text < 0 Then

            If RequestQuantity3TxtBx.Text = 0 Then
                MessageBox.Show("Quantity = 0")
            Else
                '------if quantity below 0
                MessageBox.Show("Error: Quantity below 0")
            End If
        Else
            '------if quantity is equals to 1
            MessageBox.Show("Accepted")
        End If
    End If
Else
    MessageBox.Show("ERROR")
End If

2 个答案:

答案 0 :(得分:0)

您可以在“文本框”中输入一些“验证”,以确保用户输入数字而不是字符。

Private Sub RequestQuantityTxtBx_TextChanged(sender As Object, e As EventArgs) Handles RequestQuantityTxtBx.TextChanged
If Not IsNumeric(RequestQuantityTxtBx.Text) Then
    MsgBox("This is not a number, please enter a number!")
End Sub

Private Sub RequestQuantity2TxtBx_TextChanged(sender As Object, e As EventArgs) Handles RequestQuantity2TxtBx.TextChanged
    If Not IsNumeric(RequestQuantityTxtBx.Text) Then
        MsgBox("This is not a number, please enter a number!")
    End Sub

然后,您可以放置​​IF ELSE语句。

IF RequestQuantity3TxtBx.Text > 0 THEN
    MsgBox("Accepted")
ELSEIF RequestQuantity3TxtBx.Text = 0 THEN
    MsgBox("Quantity is 0")
ELSIF RequestQuantity3TxtBx.Text < 0 THEN
    MsgBox("ERROR: Quantity below zero")
ELSE
    MsgBox("ERROR: It's not a number")
END IF

或者,您可以仅以此替换代码。

If IsNumeric(RequestQuantityTxtBx.Text) Then
    num2 = RequestQuantityTxtBx.Text
    IF IsNumeric(RequestQuantity2TxtBx.Text)
        num1 = RequestQuantity2TxtBx.Text
        total = num1 - num2
        RequestQuantity3TxtBx.Text = total

        IF RequestQuantity3TxtBx.Text > 0 THEN
            MsgBox("Accepted")
        ELSEIF RequestQuantity3TxtBx.Text = 0 THEN
            MsgBox("Quantity is 0")
        ELSE RequestQuantity3TxtBx.Text < 0 THEN
            MsgBox("ERROR: Quantity below zero")
        END IF
    ELSE
            MsgBox("ERROR: It's not a number")
    END IF
ELSE
            MsgBox("ERROR: It's not a number")
END IF

答案 1 :(得分:0)

此代码可能是满足您要求的另一种选择。

' Validate if the inputs are numeric
    If IsNumeric(RequestQuantityTxtBx.Text) AndAlso IsNumeric(RequestQuantityTxtBx2.Text) Then
        ' Cast the input to double to accomodate decimal input
        Dim num1 As Double = CType(RequestQuantityTxtBx.Text, Double)
        Dim num2 As Double = CType(RequestQuantityTxtBx2.Text, Double)

        ' Sum the input and assign the resul to the RequestQuantityTxtBx3
        Dim num3 As Double = num1 + num2
        RequestQuantityTxtBx3.Text = num3

        ' Perform validation to show appropriate message in the messagebox
        If num3 > 0 Then
            MsgBox("Accepted")
        ElseIf num3 = 0 Then
            MsgBox("Quantity is 0")
        ElseIf num3 < 0 Then
            MsgBox("Quantity below 0")
        End If
    Else
        ' Display error if the needed input is not numeric
        MsgBox("Error")
    End If

希望它会有所帮助。谢谢。