2016-11-09 6 views
-1

入力を検証しようとしています。私は何が欠けているのか分からない。私はそれをテストするときに動作します。エラーメッセージを表示しますが、計算は続けます。私はそれが(Dim blnOK As Boolean = True)と関係があると確信しています。誰も私が入力を検証して、エラーメッセージを伝えるのではなく、正しい情報を入力して停止させることができます。入力が悪いデータの場合は計算したくありません。VBでの入力の妥当性確認

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 

は、私は私がブールで私の検証を確認することができ 「検証入力 検証(decFallTime)

 If blnOK = True Then 


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

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

     End If 

にClickイベントを設定するために必要なことを考え出したし、その後の計算を行います。

+0

発信者機能コードを表示してください。 – shanyour

+0

関数を使用してFallTimeを 'lngFallTime = Validation(FallTime)'に設定し、Dim blnOK Asの次の行blnOK = True –

+0

ブール値を返す関数にすると、呼び出しコードは帰りにつまり、それは妥当性検査以上のことをしており、フラグを設定し、有効な値を解析し、制御手順として機能します。 – Plutonix

答えて

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