Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
Control array help needed
View previous topic :: View next topic  
Author Message
chrisrabb
Newbie


Joined: 20 May 2005
Posts: 5

PostPosted: Apr 11th, 2006 06:32 PM    Post subject: Control array help needed Reply with quote

Hi, I'm creating a game called Walk the Plank for my college course (for the 2nd time after failing last year), and I'm struggling with control arrays.

The form I have has the letters of the alphabet on a control array named cmdAlpha, and a hidden word is contained in a series of labels that are in another control array named lblLetter. The idea is to guess what letters are in the word by clicking the corresponding letter, and the program then searches through the lblLetter to see if it is in the word.

What should happen is if the letter is in the word, it replaces the asterisk, and the player continues guessing. If the guess is wrong, a message box comes up telling so, and the player loses 10 points from a 140 score total.

But what does happen is the error message appears whether the letter is in the word or not. See if you guys can fathom it out because I can't Sad

Code:

Dim LetterPos, WordSize, MyLoop As Integer

Private Sub cmdAlpha_Click(Index As Integer)

cmdAlpha(Index).Enabled = False

strLetter = cmdAlpha(Index).Caption

'MsgBox strLetter

LetterPos = InStr(lblLetter(0).Caption, strLetter)

'MsgBox LetterPos

If LetterPos > 0 Then

lblLetter(LetterPos).Caption = strLetter

Else

MsgBox "Wrong. Have another try.", vbCritical

End If

End Sub

Thanks in advance, you guys helped me out a lot last year and I came here first for help.

Chris
_________________
All of what I said above is 99% truthful and correct. It's the other 3% that confuses people.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
i_am_doofus
Regular


Joined: 04 Jun 2005
Posts: 57

PostPosted: Apr 12th, 2006 08:23 AM    Post subject: Reply with quote

Code:
Dim LetterPos As Integer
Dim strLetter As String
Dim correct_guess As Boolean  ' check variable to see if guess was correct
Dim i As Integer              ' counter variable to loop through control array

Private Sub cmdAlpha_Click(Index As Integer)

    ' disable button so letter can't be guessed again
    cmdAlpha(Index).Enabled = False
   
    ' set letter to check for
    strLetter = cmdAlpha(Index).Caption

    ' check each letter in the hidden word to see if letter guessed matches
    For i = 0 To lblLetter.UBound
        If strLetter = lblLetter(i).Caption Then
            correct_guess = True
            lblLetter(i).Visible = True
        End If
    Next

    ' if check variable was not set to true, tell user they were wrong
    If correct_guess = False Then
        MsgBox "Wrong. Have another try.", vbCritical
    End If

    ' reset check variable
    correct_guess = False
End Sub

Private Sub Form_Load()
    ' hide letter array
    For i = 0 To lblLetter.UBound
        lblLetter(i).Visible = False
    Next i
   
    ' initialize check variable
    correct_guess = False
End Sub
Back to top
View user's profile Send private message
chrisrabb
Newbie


Joined: 20 May 2005
Posts: 5

PostPosted: Apr 12th, 2006 08:56 AM    Post subject: Reply with quote

I tried to use that, I understand what it is meant to do, but for some reason the label just doesn't recognise what I've clicked and is returning the incorrect msgbox everytime.

A common error I keep getting aswell is that I have 12 labels in the lblLetter control array, and 26 buttons in the cmdalpha array. But if I click a letter that has an index higher than the number of labels, it doesn't like it. eg "Control array element 16 doesn't exist."
_________________
All of what I said above is 99% truthful and correct. It's the other 3% that confuses people.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
i_am_doofus
Regular


Joined: 04 Jun 2005
Posts: 57

PostPosted: Apr 12th, 2006 05:35 PM    Post subject: Reply with quote

The code I posted tested just fine for me and I never got any of the errors you're talking about. Maybe you could post your code and we can see where the problem lies.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic