I want to simplify this if then statement but when I use if isNumeric (test) and (test) it gives me an error because of the datatype. I am very new to VB and would appreciate some guidance. I have several of these text boxes I want to validate with isNumberic.
我想简化这个如果然后声明,但当我使用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
End Class
1 个解决方案
#1
0
Probably the best way to simplify your code is to get rid of those textboxes and replace them with maskedtextboxes. This way you can auto-validate all the inputs before you do any calculations.
简化代码的最佳方法可能是删除那些文本框并用maskedtextboxes替换它们。这样,您可以在进行任何计算之前自动验证所有输入。
Second best would probably be to have each one use the same validating event handler and set the e.Cancel to True if the validation fails to simply return the focus back to the offending textbox. The focus won't leave until the input is valid.
第二好的可能是让每个人使用相同的验证事件处理程序并将e.Cancel设置为True,如果验证无法简单地将焦点返回到违规文本框。在输入有效之前,焦点不会消失。
Either way with your inputs validated you can simply set the variables that need setting.
无论哪种方式验证您的输入,您只需设置需要设置的变量。
#1
0
Probably the best way to simplify your code is to get rid of those textboxes and replace them with maskedtextboxes. This way you can auto-validate all the inputs before you do any calculations.
简化代码的最佳方法可能是删除那些文本框并用maskedtextboxes替换它们。这样,您可以在进行任何计算之前自动验证所有输入。
Second best would probably be to have each one use the same validating event handler and set the e.Cancel to True if the validation fails to simply return the focus back to the offending textbox. The focus won't leave until the input is valid.
第二好的可能是让每个人使用相同的验证事件处理程序并将e.Cancel设置为True,如果验证无法简单地将焦点返回到违规文本框。在输入有效之前,焦点不会消失。
Either way with your inputs validated you can simply set the variables that need setting.
无论哪种方式验证您的输入,您只需设置需要设置的变量。