首页 > 解决方案 > 索引超出了数组的范围,但标签在那里

问题描述

我正在开发一个刽子手游戏,我有一个名为“panelword”的面板,它有 11 个标题为“label#”的标签来保存字母值。我为 FindLabel 创建了一个方法,并在其他方法中使用它来查找标签。我收到一个错误“索引超出了数组的范围”,但它关闭了,因为我可以在面板中看到标签。我是 vb 的新手,真的可以使用帮助。

Public Class GameForm


Dim dsQuestions As New HangmanDBDataSet
Dim numWord As Integer
Dim strWord As String
Dim numWrongGuesses, numRightGuesses As Integer
Dim blnGameStarted As Boolean


Private Sub GameForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    txtGuess.Enabled = False
    numWord = 0
    LoadData()
End Sub

Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitStripMenuItem.Click
    Application.Exit()
End Sub

Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles NewStripMenuItem.Click
    'Initialize game values
    numRightGuesses = 0
    numWrongGuesses = 0
    numWord += 1
    blnGameStarted = True

    'Initialize controls
    ResetAllLabels()
    txtGuess.Enabled = True
    txtGuess.Focus()

    'Open Database
    'Gets word from the rows
    strWord = CStr(dsQuestions.Words(numWord)(1))
    'Gets the description of the word category
    lblUnitDescription.Text = CStr(dsQuestions.Words(numWord).UnitsRow(1))

    'Split the word into letters and display labels
    Dim letters = strWord.ToCharArray()
    For i As Integer = 0 To letters.Length - 1
        'Activate only the used label
        FindLabel(i).Text = "___"
    Next
End Sub

Private Sub SetRightLetterLabels(ByVal strWord As String, ByVal ch As Char)
    For i As Integer = 0 To strWord.Length - 1
        ' Check for each letter and set the appropriate letter
        If strWord.Chars(i) = ch Then
            FindLabel(i).Text = ch
        End If
    Next

End Sub

Private Function FindLabel(ByVal i As Integer) As Control
    ' Finds the right label in the word panel
    Dim label = PanelWord.Controls.Find("Label", False)(0)
    Return label
End Function



Public Sub ResetAllLabels()
    Label2.Text = String.Empty
    Label3.Text = String.Empty
    Label4.Text = String.Empty
    Label5.Text = String.Empty
    Label6.Text = String.Empty
    Label7.Text = String.Empty
    Label8.Text = String.Empty
    Label9.Text = String.Empty
    Label10.Text = String.Empty
    Label11.Text = String.Empty
    Label12.Text = String.Empty
End Sub

Public Sub LoadData()
    Dim taUnits As New HangmanDBDataSetTableAdapters.UnitsTableAdapter
    Dim taWords As New HangmanDBDataSetTableAdapters.WordsTableAdapter
    taUnits.Fill(dsQuestions.Units)
    taWords.Fill(dsQuestions.Words)
End Sub

Private Sub txtGuess_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGuess.TextChanged
    txtGuess.Text = txtGuess.Text.ToLower() 'So that case-sensivity is not an issue
    If blnGameStarted Then
        If Not String.IsNullOrWhiteSpace(txtGuess.Text) And strWord.Contains(txtGuess.Text) Then
            SetRightLetterLabels(strWord, txtGuess.Text.Chars(0))
            numRightGuesses += 1
        Else
            PictureBox1.ImageLocation = "images\Hangman-" & numWrongGuesses & ".png"
            numWrongGuesses += 1
        End If
        CheckProgress()
        txtGuess.SelectAll()
    End If
End Sub

Private Sub CheckProgress()
    Dim totalTries As Integer = numRightGuesses + numWrongGuesses
    Dim newGame As DialogResult = DialogResult.None
    If WonGame() Then
        newGame = MessageBox.Show("You won in " & totalTries & " tries!" &
                         vbCrLf & vbCrLf & "Do you want to play another game?", "You won!",
                         MessageBoxButtons.YesNo, MessageBoxIcon.Information)
    ElseIf LostGame() Then
        newGame = MessageBox.Show("You lost in " & totalTries & " tries. The word was '" _
                         & strWord & "'" & vbCrLf & vbCrLf &
                       "Do you want to play another game?", "You lost",
                       MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
    End If
    If newGame = DialogResult.Yes Then
        blnGameStarted = False
        NewStripMenuItem.PerformClick()
    ElseIf newGame = DialogResult.No Then
        Application.Exit()
    End If
End Sub

Private Function WonGame() As Boolean
    Dim won As Boolean = True
    For i As Integer = 0 To strWord.Length - 1
        'Check to see if every letter matches
        won = won And FindLabel(i).Text = strWord.Chars(i)
    Next
    Return won
End Function

Private Function LostGame() As Boolean
    'If number of wrong guesses is greater than 6 then returns true
    If numWrongGuesses > 6 Then
        Return True
    Else
        Return False
    End If
End Function

结束类

标签: arraysvb.netcontrols

解决方案


推荐阅读