如何在vb.net中检查给定字段的值是否为负数(大于零)

时间:2014-02-24 07:08:48

标签: vb.net

条件(小于零)在以下代码中不起作用:

 Protected Sub txt_business_revenue_risk_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_business_revenue_risk.TextChanged

    Dim a As double= 0.0

    If IsNumeric(Me.txt_business_revenue_risk.Text) Then
        Me.lbl_business_revenue_risk.Text = ""
        Me.lbl_rest_risk.Text = CDbl(Me.txt_business_revenue_risk.Text) - CDbl(Me.txt_mitigated_risk.Text)

        If CDbl(Me.txt_business_revenue_risk.Text) < CDbl(a) Then
            Me.lbl_business_revenue_risk.Text = "Must be a number greater than zero!"
            txt_business_revenue_risk.Text = ""

        End If

        If CDbl(Me.txt_business_revenue_risk.Text) < CDbl(Me.txt_mitigated_risk.Text) Then
            Me.lbl_rest_risk.Text = 0
        Else
            Me.lbl_rest_risk.Text = CDbl(Me.txt_business_revenue_risk.Text) - CDbl(Me.txt_mitigated_risk.Text)
        End If


    Else
        Me.lbl_business_revenue_risk.Text = "Must be a number!"
        txt_business_revenue_risk.Text = ""


    End If

End Sub

2 个答案:

答案 0 :(得分:1)

试试这个 - 别忘了添加异常处理

    Dim resMsg As String = ""
    Dim txtRevRisk As String = Me.txt_business_revenue_risk.Text
    Dim txtMitRisk As String = Me.txt_mitigated_risk.Text

    Dim dRes As Double = 0
    Dim a As Double = 0.0

    If IsNumeric(txtRevRisk) AndAlso IsNumeric(txtMitRisk) Then

        Dim dRevRisk = CDbl(txtRevRisk)
        Dim dMitRisk = CDbl(txtMitRisk)

        resMsg = txtRevRisk
        dRes = dRevRisk - dMitRisk

        If dRevRisk < a Then
            resMsg = "Must be a number greater than zero!"
            txtRevRisk = ""

        End If

        If dRevRisk < dMitRisk Then
            dRes = 0
            'Else
            '    Me.lbl_rest_risk.Text = dRevRisk - dMitRisk
        End If

    Else
        resMsg = "Must be a number!"
        txtRevRisk = ""

    End If

    Me.lbl_rest_risk.Text = dRes.ToString()
    Me.lbl_business_revenue_risk.Text = resMsg.ToString()
    Me.txt_business_revenue_risk.Text = txtRevRisk.ToString()

答案 1 :(得分:1)

避免使用诸如IsNumeric之类的VB6方法,以及那些大量的双解析,试试这个:

Private Sub txt_business_revenue_risk_TextChanged(sender As Object, e As EventArgs) _
Handles txt_business_revenue_risk.TextChanged

    Dim Zero As Double = 0.0
    Dim Num1 As Double, Num2 As Double

    If Double.TryParse(CStr(sender.text), Num1) _
    AndAlso Double.TryParse(CStr(txt_mitigated_risk.Text), Num2) Then

        Me.lbl_business_revenue_risk.Text = String.Empty
        Me.lbl_rest_risk.Text = CStr(Num1 - Num2)

        Select Case Num1

            Case Is < Zero
                Me.lbl_business_revenue_risk.Text = "Must be a number greater than zero!"
                sender.Text = String.Empty

            Case Is < Num2
                Me.lbl_rest_risk.Text = CStr(Zero)

            Case Else
                Me.lbl_rest_risk.Text = CStr(Num1 - Num2)

        End Select

    Else

        Me.lbl_business_revenue_risk.Text = "Must be a number!"
        txt_business_revenue_risk.Text = String.Empty

    End If

End Sub