如果strLetter不是数字,我如何验证?

时间:2014-07-02 16:54:40

标签: vb.net

我已经尝试将所有代码放入if-else语句和另一个do循环中。无论哪种方式,只要我输入数字到inputBox,它就会结束程序。我如何让它继续下去?

Private Sub mnuFileNew_Click(sender As Object, e As EventArgs) Handles mnuFileNew.Click

    Dim strWord As String
    Dim strLetter As String
    Dim blnValidWord As Boolean
    Dim blnDashReplaeced As Boolean
    Dim blnGameOver As Boolean
    Dim intIncorrect As Integer

    ' Hide the picture boxes
    picBottom.Visible = False
    picPost.Visible = False
    picTop.Visible = False
    picRope.Visible = False
    picHead.Visible = False
    picBody.Visible = False
    picRightArm.Visible = False
    picLeftArm.Visible = False
    picRightLeg.Visible = False
    picLeftLeg.Visible = False

    ' get a 5-letter word from player 1 and convert to uppercase
    strWord = InputBox("Enter a word up to ten letters:", "Hangman Game").ToUpper

    ' determine whether the word contains 5 letters
    blnValidWord = True ' assume the word is valid
    If strWord.Length >= 10 Then
        blnValidWord = False
    Else
        Dim intIndex As Integer
        Do While intIndex >= 10 AndAlso blnValidWord = True
            If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then
                blnValidWord = False
            End If
            intIndex = intIndex + 1
        Loop
    End If

    If blnValidWord = False Then
        MessageBox.Show("Enter a word up to ten letters.", "Hangman Game", MessageBoxButtons.OK, MessageBoxIcon.Information)

    Else

        Select Case strWord.Length
            Case 1
                lblWord.Text = "-"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 2
                lblWord.Text = "--"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 3
                lblWord.Text = "---"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 4
                lblWord.Text = "----"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 5
                lblWord.Text = "-----"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 6
                lblWord.Text = "------"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 7
                lblWord.Text = "-------"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 8
                lblWord.Text = "--------"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 9
                lblWord.Text = "---------"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
            Case 10
                lblWord.Text = "----------"
                ' display five dashes in lblWord and clear lblIncorrect
                lblIncorrect.Text = String.Empty
        End Select

        ' get a letter from player 2 and convert to uppercase
        strLetter = InputBox("Enter a letter:", "Letter", "", 600, 400).ToUpper
        ' verify that player 2 entered a letter
        ' and that the games is not over
        Do While strLetter <> String.Empty AndAlso blnGameOver = False
            ' Search the word for the letter
            Dim intWordCount As Integer = strWord.Length - 1
            For intIndex As Integer = 0 To intWordCount
                ' if the letter appears in the word, then
                ' replace the dash in lblWord and
                ' indicate that a replacement was made
                If strWord.Substring(intIndex, 1) = strLetter Then

                    lblWord.Text = lblWord.Text.Remove(intIndex, 1)
                    lblWord.Text = lblWord.Text.Insert(intIndex, strLetter)
                    blnDashReplaeced = True

                End If
            Next intIndex

            ' determine whether a dash was replaced
            If blnDashReplaeced = True Then
                ' if the word does not contain any dashes,
                ' the game is over because player 2
                ' guessed the word; otherwise, reset the 
                ' blnDashReplaced variable for the next search
                If lblWord.Text.Contains("-") = False Then
                    blnGameOver = True
                    MessageBox.Show("Great guessing!", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    blnDashReplaeced = False
                End If
            Else ' processed when no dash was replaced
                ' display the incorrect letter, then update
                ' the intIncorrect variable, then show
                ' the appropriate picture box
                lblIncorrect.Text = lblIncorrect.Text & " " & strLetter
                intIncorrect = intIncorrect + 1
                Select Case intIncorrect
                    Case 1
                        picBottom.Visible = True
                    Case 2
                        picPost.Visible = True
                    Case 3
                        picTop.Visible = True
                    Case 4
                        picRope.Visible = True
                    Case 5
                        picHead.Visible = True
                    Case 6
                        picBody.Visible = True
                    Case 7
                        picRightArm.Visible = True
                    Case 8
                        picLeftArm.Visible = True
                    Case 9
                        picRightLeg.Visible = True
                    Case 10
                        picLeftLeg.Visible = True
                        blnGameOver = True
                        MessageBox.Show("Sorry, the word is " & strWord & ".", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End Select
            End If
            If blnGameOver = False Then

                strLetter = InputBox("Enter a letter", "Letter", "", 600, 400).ToUpper

            End If
        Loop
    End If
End Sub

结束班

2 个答案:

答案 0 :(得分:0)

在文本框的KeyPress事件中,添加:

如果Char.IsDigit(e.KeyChar)那么     e.Handled = True 结束如果

答案 1 :(得分:0)

我不知道为什么它会立即结束,但是这就是我尝试假设你一次只想要一个字母:

Dim strLetter As Char
Do Until Char.IsLetter(strLetter) = True
    strLetter = InputBox("Enter a letter:", "Letter", "", 600, 400).ToUpper
Loop

这应该只将输入限制在输入框中的第一个字符,并且会一直显示输入框,直到选中一个字母。