验证VB中的输入

时间:2016-11-09 15:30:22

标签: vb.net visual-studio

我正在尝试验证我的输入。我不确定我错过了什么。它在我测试时起作用。它将抛出错误消息,但仍然进行计算。我确定它与(Dim blnOK As Boolean = True)有关。任何人都可以帮我验证输入并使其停止并输入正确的信息,而不是仅仅给出错误消息。我不希望它计算输入是否是坏数据。

Public Class frmFallingDistance
Dim blnOK As Boolean = True

Private Sub Validation(ByRef FallTime As Decimal)

    'Validate inputs
    'Input needs to be numeric
    If IsNumeric(txtFallTime.Text) Then
        FallTime = CDec(txtFallTime.Text)
    Else
        MessageBox.Show("Please enter a numeric value")
        txtFallTime.Focus()
        txtFallTime.BackColor = Color.Yellow
        blnOK = False
        Exit Sub
    End If

    'Input can't be less than 0
    If FallTime < 0 Then
        MessageBox.Show("Please enter a numeric value 0 or greater")
        txtFallTime.Focus()
        txtFallTime.BackColor = Color.Yellow
        blnOK = False
        Exit Sub
    End If

    blnOK = True
End Sub

 Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

    ''declare local variables
    Dim decFall As Decimal          'gives variable ( t ) in equation
    Dim decFallDistance As Decimal  'gives variable ( d ) in equation
    Dim decAnswer As Decimal
    Dim decFallTime As Decimal      'validating data in txtFallTime

    'Return backcolor to white
    txtFallTime.BackColor = Color.White


    'Validate inputs
    Validation(decFallTime)

    'Run FallingDistance Function
    decAnswer = FallingDistance(decFall, decFallDistance)

    'Display falling distance in meters
    lblFallingDistance.Text = decAnswer.ToString

End Sub

我试图让它成为一个函数,但它仍然会计算总数而不是停止计算并强迫我将有效数据输入到输入中。

 Private Function Validation(ByVal FallTime As Decimal) As Decimal

    'Validate inputs
    'Input needs to be numeric
    If IsNumeric(txtFallTime.Text) Then
        FallTime = CDec(txtFallTime.Text)
    Else
        MessageBox.Show("Please enter a numeric value")
        txtFallTime.Focus()
        txtFallTime.BackColor = Color.Yellow
        blnOK = False
    End If

    'Input can't be less than 0
    If FallTime < 0 Then
        MessageBox.Show("Please enter a numeric value 0 or greater")
        txtFallTime.Focus()
        txtFallTime.BackColor = Color.Yellow
        blnOK = False
    End If

    Return CDec(blnOK = True)

End Function

我想通了我需要将Click事件设置为    '验证输入             验证(decFallTime)

        If blnOK = True Then


            'Run FallingDistance Function
            decAnswer = FallingDistance(decFall, decFallDistance)

            'Display falling distance in meters
            lblFallingDistance.Text = decAnswer.ToString

        End If

这允许我用布尔值验证我的验证,然后进行计算。

1 个答案:

答案 0 :(得分:0)

从我的理解,你希望txtFallTime接受数值。如果是这样的话,使用KeyPress验证用户输入会更容易。

Private Sub txtFallTime_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtFallTime.KeyPress
    'This enables the textbox to only accept numbers
    '8 is the ascii code for space
    '48 to 57 is the ascii code for 0-9

    If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
            MsgBox("Numeric input required")
        End If
    End If

End Sub