-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イベントを設定するために必要なことを考え出したし、その後の計算を行います。
発信者機能コードを表示してください。 – shanyour
関数を使用してFallTimeを 'lngFallTime = Validation(FallTime)'に設定し、Dim blnOK Asの次の行blnOK = True –
ブール値を返す関数にすると、呼び出しコードは帰りにつまり、それは妥当性検査以上のことをしており、フラグを設定し、有効な値を解析し、制御手順として機能します。 – Plutonix