|
-
May 14th, 2004, 12:25 PM
#1
Thread Starter
New Member
mid function problem
i have just started learning vb at college, my first assignment is make the game hangman. i have made the command buttons into control arrays with each caption a letter of the aphabet.
what i want the program to do is search the chosen word and check if the letter chosen(by command button) is in there.
i am using the mid function, but keep getting errors. my code is as follows
i will appreciate any help as i am still new to this.
Private Sub cmdLetter_Click(Index As Integer)
cmdLetter(Index).Enabled = False
guess = txtGuess.Text
Let guess = Chr(Index + 65)
For i = 0 To Len(answer)
If Mid(answer, i, 1) = guess Then
txtGuess.Text = guess
wrong = wrong + wrong = 1
Else
If wrong = 1 Then ln1.Visible = True
ElseIf wrong = 2 Then ln2.Visible = True
Else:
If wrong = 3 Then ln3.Visible = True
Else
If wrong = 4 Then ln4.Visible = True
Else
If wrong = 5 Then Sh1.Visible = True
Else
If wrong = 6 Then ln5.Visible = True
ElseIf wrong = 7 Then ln6.Visible = True
ElseIf wrong = 8 Then ln7.Visible = True
ElseIf wrong = 9 Then ln8.Visible = True
ElseIf wrong = 10 Then ln9.Visible = True
Next
-
May 14th, 2004, 12:28 PM
#2
try
Code:
For i = 0 To Len(answer)-1
also what is this?
wrong = wrong + wrong = 1
kevin
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
May 14th, 2004, 12:29 PM
#3
Why not just use the InStr() function?
VB Code:
If InStr(variablename, character) Then
-
May 14th, 2004, 12:33 PM
#4
if you just need to check if the letter is in the word....
then use Instr()
If Instr(Word,Guess) then
Yes...
Else
No....
End if
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
May 14th, 2004, 12:34 PM
#5
beat me to the punch mendhak!
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
May 14th, 2004, 12:39 PM
#6
Originally posted by [LGS]Static
beat me to the punch mendhak!
Careful... it's spiked.
-
May 14th, 2004, 12:47 PM
#7
Thread Starter
New Member
i have tried that, its still not working, what am i doing wrong ?
Private Sub cmdLet_Click(Index As Integer)
Dim word, temp, guess As String
Dim i, j, k As Integer
cmdLet(Index).Enabled = False
word = txtInput.Text
guess = Chr(Index + 65)
If InStr(word, guess) Then
txtOutput.Text = txtOutput + guess
Else
MsgBox "yes"
End If
End Sub
-
May 14th, 2004, 12:54 PM
#8
Here's a better way to do the number wrong processing.
VB Code:
Select Case wrong
Case 1
ln1.Visible = True
Case 2
ln2.Visible = True
Case 3
ln3.Visible = True
Case 4
ln4.Visible = True
Case 5
sh1.Visible = True
Case 6
ln6.Visible = True
Case 7
ln7.Visible = True
Case 8
ln8.Visible = True
Case 9
ln9.Visible = True
Case 10
ln10.Visible = True
End Select
I don't know what that sh1 is all about, but if that should really be ln5 then I suggest you change the lines into a control array. If you did that then the above could be reduced to
VB Code:
lnLine(wrong).Visible = True
I also don't understand what txtInput is used for. Are you asking the user to type in his guess? Why not just use the caption of the command button that is clicked?
-
May 14th, 2004, 12:57 PM
#9
Originally posted by vinnyonfire
i have tried that, its still not working, what am i doing wrong ?
Private Sub cmdLet_Click(Index As Integer)
Dim word, temp, guess As String
Dim i, j, k As Integer
cmdLet(Index).Enabled = False
word = txtInput.Text
guess = Chr(Index + 65)
If InStr(word, guess) Then
txtOutput.Text = txtOutput + guess
Else
MsgBox "yes"
End If
End Sub
What is not working? BTW, when you Dim like this
Dim word, temp, guess As String
Dim i, j, k As Integer
only guess and k are defined the way you want. The others are left as variants. You should do
Dim word As String
Dim temp As String
etc., or
Dim word As String, temp As String
-
May 14th, 2004, 12:59 PM
#10
Thread Starter
New Member
thanks for that martin. that code will be useful to me. the txtinput box is for the player 1 to type in a word, player 2 has to guess the word by pressing the cmdLet array, if that letter exists in the word then display in txtoutput, else draw a line
-
May 14th, 2004, 01:07 PM
#11
Your Index + 65 results in uppercase letters, so do this when checking the guess
word = UCase(txtInput.Text)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|