> =消息框顺序无法正常工作

时间:2019-01-16 06:08:07

标签: vb.net visual-studio

当“以旧换新价值”> =“价格”时,我试图弹出一个消息框。如果我输入以旧换新值:2000和价格:12,000。消息框出现,这显然不应该发生。在测试了一堆数字之后,似乎该表单不喜欢两个文本框中的数字“ 2”。第二个块是我的代码不起作用的地方。 我对编码还很陌生,这是我的第一篇文章,接受者easy boy / girls。

输入的数字/表格

Entered numbers/Form

消息框错误

Message Box error

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    GBTradeIn.Enabled = False
    TxtTradeIn.Enabled = False
    RBStandard.Checked = True
    RBExcellent.Checked = True
End Sub
Private Sub CBTradeIn_CheckedChanged(sender As Object, e As EventArgs) Handles CBTradeIn.CheckedChanged
    GBTradeIn.Enabled = True
    TxtTradeIn.Enabled = True
    If CBTradeIn.Checked = (False) Then
        GBTradeIn.Enabled = False
        TxtTradeIn.Enabled = False
        RBExcellent.Checked = True
        TxtTradeIn.Text = ""
    End If
End Sub
Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
    Try
        If CBTradeIn.Checked = (False) Then
            TxtTradeIn.Text = 0
        End If
        If RBExcellent.Checked Then
            TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 1)
        ElseIf RBGood.Checked Then
            TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 0.9)
        ElseIf RBFair.Checked Then
            TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 0.8)
        ElseIf RBPoor.Checked Then
            TxtTradeInAllowance.Text = FormatCurrency(TxtTradeIn.Text * 0.7)
        End If
    Catch
        MessageBox.Show("Enter valid Trade-In value")
    End Try
    Dim Exterior As Decimal
    Dim Accessories As Decimal
    If CBPremiumStereo.Checked And CBLeatherInterior.Checked And CBGPS.Checked Then
        Accessories = 3154.4
    ElseIf CBPremiumStereo.Checked And CBLeatherInterior.Checked Then
        Accessories = 1413.17
    ElseIf CBPremiumStereo.Checked And CBGPS.Checked Then
        Accessories = 2166.99
    ElseIf CBGPS.Checked And CBLeatherInterior.Checked Then
        Accessories = 2728.64
    ElseIf CBPremiumStereo.Checked Then
        Accessories = 425.76
    ElseIf CBLeatherInterior.Checked Then
        Accessories = 987.41
    ElseIf CBGPS.Checked Then
        Accessories = 1741.23
    End If
    If RBStandard.Checked = True Then
        Exterior = 0
    ElseIf RBPearlized.Checked = True Then
        Exterior = 345.72
    ElseIf RBCustom.Checked = True Then
        Exterior = 599.99
    End If
    TxtAccessoriesAndFinish.Text = FormatCurrency(Accessories + Exterior)
    Try
        Dim total As Decimal
        Dim TradeIn As Decimal
        Dim AccessoriesandExterior As Decimal
        Dim Subtotal As Decimal
        total = TxtPrice.Text
        TradeIn = TxtTradeInAllowance.Text
        AccessoriesandExterior = TxtAccessoriesAndFinish.Text
        TxtSubtotal.Text = FormatCurrency(total + AccessoriesandExterior - TradeIn)
        Subtotal = TxtSubtotal.Text
        TxtSalesTax.Text = FormatCurrency((Subtotal + TradeIn) * 0.08)
        TxtAmountDue.Text = FormatCurrency(Subtotal + TxtSalesTax.Text)
    Catch
        TxtAccessoriesAndFinish.Text = ""
        TxtAmountDue.Text = ""
        TxtTradeIn.Text = ""
        TxtSubtotal.Text = ""
        TxtSalesTax.Text = ""
        TxtPrice.Text = ""
        TxtTradeInAllowance.Text = ""
        MessageBox.Show("Enter a valid price")
    End Try

>

    If TxtTradeIn.Text >= TxtPrice.Text Then
            TxtAccessoriesAndFinish.Text = ""
            TxtAmountDue.Text = ""
            TxtTradeIn.Text = ""
            TxtSubtotal.Text = ""
            TxtSalesTax.Text = ""
            TxtPrice.Text = ""
        TxtTradeInAllowance.Text = ""

        MessageBox.Show("Trade in value can't be higher than or equal to price")
        End If
        If CBTradeIn.Checked = (False) Then
        TxtTradeIn.Text = ""
    End If

End Sub

>

Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
    TxtAccessoriesAndFinish.Text = ""
    TxtAmountDue.Text = ""
    TxtTradeIn.Text = ""
    TxtSubtotal.Text = ""
    TxtSalesTax.Text = ""
    TxtPrice.Text = ""
    RBStandard.Checked = True
    RBExcellent.Checked = True
    CBPremiumStereo.Checked = False
    CBLeatherInterior.Checked = False
    CBGPS.Checked = False
    CBTradeIn.Checked = False
    TxtTradeInAllowance.Text = ""

End Sub

结束班级

1 个答案:

答案 0 :(得分:0)

代替

If TxtTradeIn.Text >= TxtPrice.Text Then

使用

If CDec(TxtTradeIn.Text) >= CDec(TxtPrice.Text) Then

CDec()将括号内的文本转换为小数,从而允许您执行数字比较而不是字符串比较。

但是,您必须小心:如果尝试将非十进制转换为十进制(例如字符串),CDec()将返回错误,因此您必须检查用户是否输入了有效的十进制在文本框中