Results 1 to 11 of 11

Thread: mid function problem

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    lancashire
    Posts
    3

    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

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762
    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

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Why not just use the InStr() function?

    VB Code:
    1. If InStr(variablename, character) Then

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    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"

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390
    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"

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by [LGS]Static
    beat me to the punch mendhak!

    Careful... it's spiked.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    lancashire
    Posts
    3
    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

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here's a better way to do the number wrong processing.

    VB Code:
    1. Select Case wrong
    2.             Case 1
    3.                 ln1.Visible = True
    4.             Case 2
    5.                 ln2.Visible = True
    6.             Case 3
    7.                 ln3.Visible = True
    8.             Case 4
    9.                 ln4.Visible = True
    10.             Case 5
    11.                 sh1.Visible = True
    12.             Case 6
    13.                 ln6.Visible = True
    14.             Case 7
    15.                 ln7.Visible = True
    16.             Case 8
    17.                 ln8.Visible = True
    18.             Case 9
    19.                 ln9.Visible = True
    20.             Case 10
    21.                 ln10.Visible = True
    22.         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:
    1. 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?

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2004
    Location
    lancashire
    Posts
    3
    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

  11. #11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width