如果那么isNumeric并且不起作用需要简化

时间:2013-11-16 22:03:41

标签: vb.net visual-studio-2010

我想简化这个if if语句,但是当我使用isNumeric(test)和(test)时,由于数据类型的原因,它会给我一个错误。我是VB的新手,非常感谢一些指导。我有几个我想用isNumberic验证的文本框。

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    'Declare variables for pay
    Dim decConneryPay As Decimal = 0
    Dim decLazenbyPay As Decimal = 0
    Dim decMoorePay As Decimal = 0
    Dim decDaltonPay As Decimal = 0
    Dim decBrosnanPay As Decimal = 0
    Dim decCraigPay As Decimal = 0

    'Initial clear lblPayError on each calculation
    lblPayError.Text = String.Empty


    'Convert pay rate text boxes to decimals and * with hours
    'Check txtConneryHours for validation
    If IsNumeric(txtConneryHours.Text) Then
        decConneryPay = CDec(txtRateSean.Text) * CDec(txtConneryHours.Text)
        lblConneryPay.Text = decConneryPay.ToString("c")
    Else
        lblPayError.ForeColor = Color.Red
        lblPayError.Text = "Hours worked can only contain positive integers."
    End If

    'Check txtLazenbyHours for validation
    If IsNumeric(txtLazenbyHours.Text) Then
        decLazenbyPay = CDec(txtRateLazenby.Text) * CDec(txtLazenbyHours.Text)
        lblLazenbyPay.Text = decLazenbyPay.ToString("c")
    Else
        lblPayError.ForeColor = Color.Red
        lblPayError.Text = "Hours worked can only contain positive integers."
    End If

    'Check txtMooreHours for validation
    If IsNumeric(txtMooreHours.Text) Then
        decMoorePay = CDec(txtRateMoore.Text) * CDec(txtMooreHours.Text)
        lblMoorePay.Text = decMoorePay.ToString("c")
    Else
        lblPayError.ForeColor = Color.Red
        lblPayError.Text = "Hours worked can only contain positive integers."
    End If

    'Check txtDaltonHours for validation
    If IsNumeric(txtDaltonHours.Text) Then
        decDaltonPay = CDec(txtRateDalton.Text) * CDec(txtDaltonHours.Text)
        lblDaltonPay.Text = decDaltonPay.ToString("c")
    Else
        lblPayError.ForeColor = Color.Red
        lblPayError.Text = "Hours worked can only contain positive integers."
    End If

    'Check txtBrosnanHours for validation
    If IsNumeric(txtBrosnanHours.Text) Then
        decBrosnanPay = CDec(txtRateBrosnan.Text) * CDec(txtBrosnanHours.Text)
        lblBrosnanPay.Text = decBrosnanPay.ToString("c")
    Else
        lblPayError.ForeColor = Color.Red
        lblPayError.Text = "Hours worked can only contain positive integers."
    End If

    'Check txtCraigHours for validation
    If IsNumeric(txtCraigHours.Text) Then
        decCraigPay = CDec(txtRateCraig.Text) * CDec(txtCraigHours.Text)
        lblCraigPay.Text = decCraigPay.ToString("c")
    Else
        lblPayError.ForeColor = Color.Red
        lblPayError.Text = "Hours worked can only contain positive integers."
    End If

End Sub

结束班

1 个答案:

答案 0 :(得分:0)

简化代码的最佳方法可能是摆脱那些文本框并用maskedtextboxes替换它们。这样,您可以在进行任何计算之前自动验证所有输入。

第二好的可能是让每个人使用相同的验证事件处理程序,并且如果验证无法简单地将焦点返回到违规文本框,则将e.Cancel设置为True。在输入有效之前,焦点不会消失。

无论哪种方式验证您的输入,您只需设置需要设置的变量。