Argh, visual basic express ed. 2008, hangman won't work! says that 'Index was out of range. Must be non-neg'?

When user has input too many wrong goes it crashes and comes up yellow at the line 'index = thisword.word.IndexOf(letter, start)' saying 'Index was out of range. Must be non-neg'.

Code:

Public Class frmHangman

Private Sub btnMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMenu.Click
Me.Hide()
frmMenu.Show()
End Sub
'create 'bag' variable as a new collection
Public bag As New Collection
'TEXT
Public thisword As word
Public counter As Integer = 0
Public index As Integer
Public foundLetter As Boolean = False
Structure word
'create 'word' and 'hint' as string
Public word As String
Public hint As String
End Structure


Private Sub frmHangman_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim index As Integer
Dim wordString(1) As String
'read hangman .txt file as new streamreader
Dim readthis As New System.IO.StreamReader("hangman.txt")
Do While readthis.EndOfStream = False
'split the string in text file up where commas appear
wordString = Split(readthis.ReadLine(), ",")
'first part of .txt line is for the word
thisword.word = wordString(0)
thisword.word.ToLower()
'second part of the .txt line is for the hint
thisword.hint = wordString(1)
thisword.hint.ToLower()
'add to the bag
bag.Add(thisword)
Loop
readthis.Close()
'get random question from the bag
Randomize()
index = Rnd() * (bag.Count - 1) + 1
thisword = bag.Item(index)
lblClue.Text = thisword.hint
bag.Remove(index)


For index = 1 To thisword.word.Length
'show number of dashes to represent letters
lblDisplay.Text = lblDisplay.Text & "-"
Next


End Sub

Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click, btnB.Click, btnC.Click, btnD.Click, btnE.Click, btnF.Click, btnG.Click, btnH.Click, btnI.Click, btnJ.Click, btnK.Click, btnL.Click, btnM.Click, btnN.Click, btnO.Click, btnP.Click, btnQ.Click, btnR.Click, btnS.Click, btnT.Click, btnU.Click, btnV.Click, btnW.Click, btnX.Click, btnY.Click, btnZ.Click
display(sender)
Dim index As Integer
Dim start As Integer = 0
Dim letter As String

Do
letter = sender.Text
'see if the letter combination is in the string
letter = letter.ToLower
index = thisword.word.IndexOf(letter, start)
'if it is then
If index >= 0 Then
'remove the dashes corresponding to the letters
lblDisplay.Text = lblDisplay.Text.Remove(index, letter.Length)
'and insert the letters
lblDisplay.Text = lblDisplay.Text.Insert(index, letter)
foundLetter = True

Else
If foundLetter = True Then
foundLetter = False
Else
counter = counter + 1

If counter = 1 Then
pbFrame.Show()
ElseIf counter = 2 Then
pbHead.Show()
ElseIf counter = 3 Then
pbBody.Show()
ElseIf counter = 4 Then
pbArm1.Show()
ElseIf counter = 5 Then
pbArm2.Show()
ElseIf counter = 6 Then
pbLeg1.Show()
ElseIf counter = 7 Then
pbLeg2.Show()
MessageBox.Show("too bad - run out of goes")
'start = index + 1
'start = index + 1
If bag.Count > 0 Then
Randomize()
index = Rnd() * (bag.Count - 1) + 1
thisword = bag.Item(index)
lblClue.Text = thisword.hint
bag.Remove(index)
'reset the dashes
lblDisplay.Text = ""
'show the dashes
For index = 1 To thisword.word.Length
'show number of dashes to represent letters
lblDisplay.Text = lblDisplay.Text & "-"
Next
pbArm1.Hide()
pbArm2.Hide()
pbBody.Hide()
pbFrame.Hide()
pbHead.Hide()
pbLeg1.Hide()


I get your search, index = thisword.word.IndexOf(letter, start), which is fine but don't understand why start is a variable. Won't the seach always be from the start of the word, 0? You can just put in 0. I'd imagine the error is becuase your start variable is greater than thisword.length and that somewhere your changing the start vairable and the position of the search.




What do a hangman and piano have in common?

best answer goes to the person who gets the first correct answer and/or the person who can tell me which graphic novel it

HANGMAN HELP!!! A_O_ _ _ OUS?

What was the song at the end of the Numb3rs episode Hangman? (Season 6)?

["Body shed not a breath too soon, never said it was good never said it new... etc"]. I really want to find this

Can someone help me with hangman please?

_ I_ A _ I have 3 chances left i've used e,o,u

The Year of The Hangman Help?

I have to do a book report. I was never good at these. I'm in the 10th grade.

I have to summarize the plot.

¿In GTA4 hangman's NOOSE question?

Huh...
So what do you think of the lagging problems in this multiplayer co-op mission,i been getting kick out from the

What is the answer to Qweb set 2 level 4 the one w/ hangman?



Funny Laughter Pixar Cars

Terms of Service and Privacy Policy © 2008-2010 PlayHangmanGames.com - Hangman