I am making a Game called HangMan. :cheesy: I notice someone else has also but mine is due this Monday Comeing. I am having trouble with my Arrays. I keep getting am error :mad: saying "Index was outside the bounds of the array". I have been trying to fix it for a few days now and I cant figure it out. :eek: If you can help that would be great!
~~~Here is my code below~~~


Public Class Form1
Inherits System.Windows.Forms.Form
Dim word As String
Dim arrayWord(15) As String
Dim wrongGuess As Integer
Dim myrand As Integer
Dim wordcount As Integer
Dim strWord As String()
Dim i As Integer
Dim x As Integer

Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
Call enableAll()


FileSystem.FileOpen(1, "H:\Computerterms.txt", OpenMode.Input)
' Add each line from the file to an arrray
Do While Not EOF(1)
ReDim Preserve strWord(i)
strWord(i) = FileSystem.LineInput(1)
i += 1
Loop
' Close the file
FileSystem.FileClose(1)

word = (1)

wrongGuess = 0 ' reset wrong guesses
lblWord.Text = questionmark(word.Length, "") 'Displays Question Mark
If lblWord.Text = "" Then 'Message when you run out of words
MessageBox.Show("Sorry... You have Lost due to You runing out of words...")
End If

' Initalize the random generator
Randomize()
lblWord.Text = strWord(i)
word = UCase(myrand)

lblWord.Text = ""
ReDim arrayWord(Len(myrand) = 1)
For x = 1 To Len(myrand)

Next
End Sub

'Question Mark function
Private Function questionmark(ByVal wordlength As Integer, ByVal question As String) As String
For wordlength = 0 To wordlength - 1
question += " ?"
Next

Return question
End Function

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
Call ResetAll()
End Sub

Public Sub guess(ByVal letter As String)

lblWord.Text = ""
Dim goodFlag As Boolean = False
Dim x As Integer
Dim winFlag As Boolean = True
' check to see if the letter fits
For x = 1 To Len(word)
If letter = Mid(word, x, 1) Then
arrayWord(x) = letter
goodFlag = True
End If
lblWord.Text += arrayWord(x)
Next
' see if its a winner or not
For x = 1 To Len(word)
If "?" = arrayWord(x) Then
winFlag = False
Exit For
End If
Next
If winFlag = True Then
MessageBox.Show("YOU WIN!!!!", "A WINNER!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
disableAll()
Exit Sub
End If
' put the letter in if correct
If goodFlag = False Then
lblWrong.Text += " " & letter
Call SubwrongGuess()
End If

End Sub
Public Sub SubwrongGuess()
wrongGuess += 1
lblWrongCount.Text = wrongGuess
If wrongGuess = 1 Then PictureBox10.Visible = False
If wrongGuess = 2 Then PictureBox9.Visible = False
If wrongGuess = 3 Then PictureBox8.Visible = False
If wrongGuess = 4 Then PictureBox7.Visible = False
If wrongGuess = 5 Then PictureBox6.Visible = False
If wrongGuess = 5 Then pb1.Image = ImageList1.Images(1)
If wrongGuess = 6 Then PictureBox5.Visible = False
If wrongGuess = 7 Then PictureBox4.Visible = False
If wrongGuess = 8 Then PictureBox3.Visible = False
If wrongGuess = 9 Then PictureBox2.Visible = False
If wrongGuess = 10 Then PictureBox1.Visible = False
If wrongGuess = 10 Then pb1.Image = ImageList1.Images(2)
If wrongGuess = 11 Then
pb1.Image = ImageList1.Images(3)
Call disableAll()
MessageBox.Show("You loose...")
End If
End Sub
Public Sub ResetAll()
btna.Visible = True
btnb.Visible = True
btnc.Visible = True
btnd.Visible = True
btne.Visible = True
btnf.Visible = True
btng.Visible = True
btnh.Visible = True
btni.Visible = True
btnj.Visible = True
btnk.Visible = True
btnl.Visible = True
btnm.Visible = True
btnn.Visible = True
btno.Visible = True
btnp.Visible = True
btnq.Visible = True
btnr.Visible = True
btns.Visible = True
btnt.Visible = True
btnu.Visible = True
btnv.Visible = True
btnw.Visible = True
btnx.Visible = True
btny.Visible = True
btnz.Visible = True

'txtWord.Text = ""
'txtWord.Focus()
' lblWord.Text = ""
Call disableAll()
'lblWrongcount.Text = "0"
'lblWrong.Text = ""
' pb1.Image = ImageList1.Images(0)
PictureBox1.Visible = True
PictureBox2.Visible = True
PictureBox3.Visible = True
PictureBox4.Visible = True
PictureBox5.Visible = True
PictureBox6.Visible = True
PictureBox7.Visible = True
PictureBox8.Visible = True
PictureBox9.Visible = True
PictureBox10.Visible = True

End Sub
Public Sub enableAll()
btna.Enabled = True
btnb.Enabled = True
btnc.Enabled = True
btnd.Enabled = True
btne.Enabled = True
btnf.Enabled = True
btng.Enabled = True
btnh.Enabled = True
btni.Enabled = True
btnj.Enabled = True
btnk.Enabled = True
btnl.Enabled = True
btnm.Enabled = True
btnn.Enabled = True
btno.Enabled = True
btnp.Enabled = True
btnq.Enabled = True
btnr.Enabled = True
btns.Enabled = True
btnt.Enabled = True
btnu.Enabled = True
btnv.Enabled = True
btnw.Enabled = True
btnx.Enabled = True
btny.Enabled = True
btnz.Enabled = True
End Sub
Public Sub disableAll()
btna.Enabled = False
btnb.Enabled = False
btnc.Enabled = False
btnd.Enabled = False
btne.Enabled = False
btnf.Enabled = False
btng.Enabled = False
btnh.Enabled = False
btni.Enabled = False
btnj.Enabled = False
btnk.Enabled = False
btnl.Enabled = False
btnm.Enabled = False
btnn.Enabled = False
btno.Enabled = False
btnp.Enabled = False
btnq.Enabled = False
btnr.Enabled = False
btns.Enabled = False
btnt.Enabled = False
btnu.Enabled = False
btnv.Enabled = False
btnw.Enabled = False
btnx.Enabled = False
btny.Enabled = False
btnz.Enabled = False
End Sub
'~~~~~~~~~~~~~~~~~~~~Button A-Z Click~~~~~~~~~~~~~~~~~~~~~~~~~~'
Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btna.Click
guess(btna.Text)
btna.Visible = False
End Sub

Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnb.Click
guess(btnb.Text)
btnb.Visible = False
End Sub

Private Sub btnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnc.Click
guess(btnc.Text)
btnc.Visible = False
End Sub

Private Sub btnD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnd.Click
guess(btnd.Text)
btnd.Visible = False
End Sub

Private Sub btnE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btne.Click
guess(btne.Text)
btne.Visible = False
End Sub

Private Sub btnF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnf.Click
guess(btnf.Text)
btnf.Visible = False
End Sub

Private Sub btnG_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btng.Click
guess(btng.Text)
btng.Visible = False
End Sub

Private Sub btnH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnh.Click
guess(btnh.Text)
btnh.Visible = False
End Sub

Private Sub btnI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btni.Click
guess(btni.Text)
btni.Visible = False
End Sub

Private Sub btnJ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnj.Click
guess(btnj.Text)
btnj.Visible = False
End Sub

Private Sub btnK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnk.Click
guess(btnk.Text)
btnk.Visible = False
End Sub

Private Sub btnL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnl.Click
guess(btnl.Text)
btnl.Visible = False
End Sub

Private Sub btnM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnm.Click
guess(btnm.Text)
btnm.Visible = False
End Sub

Private Sub btnN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnn.Click
guess(btnn.Text)
btnn.Visible = False
End Sub

Private Sub btnO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btno.Click
guess(btno.Text)
btno.Visible = False
End Sub

Private Sub btnP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnp.Click
guess(btnp.Text)
btnp.Visible = False
End Sub

Private Sub btnQ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnq.Click
guess(btnq.Text)
btnq.Visible = False
End Sub

Private Sub btnR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnr.Click
guess(btnr.Text)
btnr.Visible = False
End Sub

Private Sub btnS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btns.Click
guess(btns.Text)
btns.Visible = False
End Sub

Private Sub btnT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnt.Click
guess(btnt.Text)
btnt.Visible = False
End Sub

Private Sub btnU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnu.Click
guess(btnu.Text)
btnu.Visible = False
End Sub

Private Sub btnV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnv.Click
guess(btnv.Text)
btnv.Visible = False
End Sub

Private Sub btnW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnw.Click
guess(btnw.Text)
btnw.Visible = False
End Sub

Private Sub btnX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnx.Click
guess(btnx.Text)
btnx.Visible = False
End Sub

Private Sub btnY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btny.Click
guess(btny.Text)
btny.Visible = False
End Sub

Private Sub btnZ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnz.Click
guess(btnz.Text)
btnz.Visible = False
End Sub


End Class

I don't have vb.net. I have vb6 (which, in my opinion is better). I realize this doesn't help you with your problem, so take into consideration this idea. While logically, you would expect the LEN function to return the number of elements in your array... in my experience... it does not. (I find that it returns the length of the value of one of the index's). I think your best bet, when working with array's element counts, is to use "UBOUND" which will return the amount of elements in the array.

I'm sorry I couldn't paste your code, and run it... so that I could further pinpoint your particular situation, and I hope that what I have offered gives some help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.