0

メッセージ ボックスを使用して、ユーザーが次のフォームに進むのを止めようとしていますが、何が問題なのかよくわかりません。メッセージ ボックスがポップアップしますが、それでも次のフォームに変わります。何か助けてください。

編集: ご協力ありがとうございます。現在、年齢チェックが機能していないため、任意の年齢を入力でき、エラーは表示されません。何か案は。変更されたすべてのコードを最後に置きます

Dim errorcount As Integer = 0

Private Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click

    strName = txtName.Text
    strAddress = rtfAddress.Text
    strCity = txtCity.Text
    strEmail = txtEmail.Text
    strHomePhone = mtxtHomePhone.Text
    strMobilePhone = mtxtMobilePhone.Text

    If txtName.Text = "" Then
        MessageBox.Show("You must enter full name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A

    ElseIf IsNumeric(txtName.Text) Then
        MessageBox.Show("Please enter a valid name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If txtAge.Text = "" Then
        MessageBox.Show("You must enter your age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
        'Else
        '    strAge = Integer.Parse(txtAge.Text)

    End If

    'Declaring age check
    Dim AgeCheck As Boolean = False

    If IsNumeric(txtAge.Text) = True Then
        AgeCheck = True

        strAge = Integer.Parse(txtAge.Text)
    ElseIf strAge < 18 Then
        MessageBox.Show("You must be over 18 years old", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False

    ElseIf strAge > 125 Then
        MessageBox.Show("Don't be stupid. You're not that old.", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False

    ElseIf AgeCheck = False Then
        MessageBox.Show("You must enter a valid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
    End If

    If Not IsNumeric(txtAge.Text) Then
        MessageBox.Show("Please enter a vadid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If IsNumeric(rtfAddress.Text) Or rtfAddress.Text = "" Then
        MessageBox.Show("Please enter your address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If IsNumeric(txtCity.Text) Or txtCity.Text = "" Then
        MessageBox.Show("Please enter your town/city", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    'if no index hasd been selected
    If cmbCounty.SelectedItem = "" Then
        MessageBox.Show("Please select a county", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If Not mtxtHomePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid home phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If Not mtxtMobilePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid mobile phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

    If txtEmail.Text = "" Then
        MessageBox.Show("You must enter a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A

    ElseIf Not txtEmail.Text.Contains("@") Then
        MessageBox.Show("Not a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        btnNext.Enabled = False
        errorcount += 1
        GoTo A
    End If

A:
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
        End
    End If

        Me.Hide()
        frmCreditCardInfo.Show()

End Sub

*整数としてのエラーカウントを薄暗くする = 0

Private Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click
    strName = txtName.Text
    strAddress = rtfAddress.Text
    strCity = txtCity.Text
    strEmail = txtEmail.Text
    strHomePhone = mtxtHomePhone.Text
    strMobilePhone = mtxtMobilePhone.Text
    If txtName.Text = "" Then
        MessageBox.Show("You must enter full name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    ElseIf IsNumeric(txtName.Text) Then
        MessageBox.Show("Please enter a valid name", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If txtAge.Text = "" Then
        MessageBox.Show("You must enter your age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    'Declaring age check
    Dim AgeCheck As Boolean = False
    If IsNumeric(txtAge.Text) = True Then
        AgeCheck = True
        'strAge = Integer.Parse(txtAge.Text)
    ElseIf strAge < 18 Then
        MessageBox.Show("You must be over 18 years old", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
    ElseIf strAge > 125 Then
        MessageBox.Show("Don't be stupid. You're not that old.", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
    ElseIf AgeCheck = False Then
        MessageBox.Show("You must enter a valid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        Exit Sub
    End If
    If Not IsNumeric(txtAge.Text) Then
        MessageBox.Show("Please enter a vadid age", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
    End If
    If IsNumeric(rtfAddress.Text) Or rtfAddress.Text = "" Then
        MessageBox.Show("Please enter your address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If IsNumeric(txtCity.Text) Or txtCity.Text = "" Then
        MessageBox.Show("Please enter your town/city", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    'if no index hasd been selected
    If cmbCounty.SelectedItem = "" Then
        MessageBox.Show("Please select a county", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If Not mtxtHomePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid home phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If Not mtxtMobilePhone.MaskCompleted Then
        MessageBox.Show("Please enter a valid mobile phone number", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If txtEmail.Text = "" Then
        MessageBox.Show("You must enter a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
    ElseIf Not txtEmail.Text.Contains("@") Then
        MessageBox.Show("Not a valid email address", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
        errorcount += 1
        Exit Sub
    End If
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
        Me.Dispose()
        Exit Sub
        'End
        'Exit Sub
    End If
    Me.Hide()
    frmCreditCardInfo.Show()
End Sub

終了クラス*

4

5 に答える 5

0

コードにはかなりの数の論理エラーがあります。たとえば、次のようになります。

If IsNumeric(txtAge.Text) = True Then
  AgeCheck = True
  strAge = Integer.Parse(txtAge.Text)
ElseIf strAge < 18 Then

txtAge.Textまず、数値であることを確認します。はいの場合は、同じ文字列に解析します。なんで?もう数値です。まあ、実際にIsNumericは小数を意味することもあります。しかし、解析を 1 回ではなく 2 回行っても意味がありません。次に、数値でない場合は、 に進みますElseIf。今strAge = "AAA"、文字列比較を行っているとします"AAA" < 18。なぜ悪いのか、以下を参照してください。

Option Strict Offそうしないと、コンパイラーがまさにこの行であなたに怒鳴るからです。問題は、比較が暗黙的に になる"AAA" < "18"ことですFalse(イミディエイト ウィンドウを使用して確認できます)。ただし、 に達する"AAA" > "125"と、このステートメントは実際には になりTrueます。そして、次のメッセージが表示されます:Don't be stupid. You're not that old.入力とは関係ありません。年齢チェックはそのようには機能しません。Option Strict Onファイルごと、またはプロジェクト全体でをオンにしてください。コードの何カ所にコードの匂いがあるかがわかります。

動作するコードを取得したい場合は、コードで実行したいことを(コードではなく) 言葉で説明してください。ベスト プラクティスに従って、コード サンプルを作成して学習できるようにします。 . ええ、使用しないでくださいGoTo's。ほとんど必要ありません。

于 2013-12-09T14:35:36.497 に答える
0

フォームをシャットダウンする前に、最終的なエラー メッセージを表示したいと思いますか?

現在の問題は、エラー メッセージが表示されても、その時点でコードを壊すものがないため、ステートメントを実行して次のフォームを表示することです ( frmCreditCardInfo.Show())。これに対する解決策は次のとおりです。

変化する:

A:
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
        End
    End If

    Me.Hide()
    frmCreditCardInfo.Show()

End Sub

に:

A:
    If errorcount >= 5 Then
        MessageBox.Show("Too many errors. Shutting down", "Error", MessageBoxButtons.OK, _
                    MessageBoxIcon.Warning)
        Me.Dispose()
        Exit Sub
    End If

    Me.Hide()
    frmCreditCardInfo.Show()

End Sub

エラーメッセージボックスを表示するのに十分なビルドGoTo Aを許可せずにコードを直接そこに送信するため、ステートメントのいくつか (またはすべて) を削除することもできます (または、これらを即時の失敗セットにしたい場合は、前にステートメント 。errorcounterrorcount = 5GoTo A

于 2013-12-09T07:57:31.623 に答える
0

サブルーチンを中断するには、すべてGoTo Aをに変更しますExit Sub

于 2013-12-09T05:04:30.573 に答える