Results 1 to 33 of 33

Thread: [RESOLVED] help with hangman

  1. #1

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Resolved [RESOLVED] help with hangman

    I need help with my hangman program.... i am trying to make this load using control arrays at run time... can someone please tell me if the file i have attached uses control arrays that load at runtime? and if it doesnt can you please tell me what i have to do to make it have control arrays at runtime...

    the sooner the better...thank you.
    Attached Files Attached Files
    Last edited by coder500; Dec 7th, 2005 at 08:07 PM.

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    Welcome to the Forum coder500

    It appeared that it may use a Control Array (due to txtLetter(0))

    In any case try this to see that additional TextBox's are added.
    VB Code:
    1. Private Sub cmdInputWord_Click()
    2.     Dim i As Integer
    3.     Dim word As String
    4.     Dim guess As String
    5.        
    6.     word = txtWord.Text
    7.     txtWord.Visible = False
    8.     'cmdInputWord.Visible = False
    9.    
    10.     txtWord.Text = UCase(word)
    11.     'guess = txtLetter(0).Text CANGED TO:
    12.     guess = txtLetter(txtLetter.Count - 1).Text
    13.    
    14.     'word = txtLetter(0).Index < ???
    15.    
    16.     Load txtLetter(txtLetter.Count)
    17.     txtLetter(txtLetter.Count - 1).Top = txtLetter(0).Top
    18.     txtLetter(txtLetter.Count - 1).Left = txtLetter(txtLetter.Count - 2).Left + txtLetter(txtLetter.Count - 1).Width
    19.     txtLetter(txtLetter.Count - 1).Text = vbNullString
    20.     txtLetter(txtLetter.Count - 1).Visible = True
    21.  
    22. End Sub

  3. #3
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help with hangman

    Quote Originally Posted by coder500
    can someone please tell me if the file i have attached uses control arrays that load at runtime?
    No. The form doesn't uses control array at runtime.

    Quote Originally Posted by coder500
    what i have to do to make it have control arrays at runtime...
    The easiest method may be,
    1. Add a control during designtime.
    2. From Properties window, change it's Index = 0. This will make it the first element of the control array.
    3. Now from code you can add new elements using the Load statement. (see the code below)
    4. Please read the documentation of Load, UBound, LBound, Count, Item methods/properties from MSDN.

    --------------------
    The following code places a new Textbox (control array element) at random places on the form
    VB Code:
    1. 'Place this code in a form and add a CommandButton and a TextBox
    2. 'Change the TextBox's Index to 0
    3. Private Sub Command1_Click()
    4.  
    5. Load Text1(Text1.UBound + 1) 'Loads new control array element
    6.  
    7. With Text1(Text1.UBound)
    8.    
    9.   .Left = CInt((Me.Width + 1) * Rnd) 'Set a random Left
    10.   .Top = CInt((Me.Height + 1) * Rnd) 'Set a random Top
    11.  
    12.   .BackColor = QBColor(CInt((15 * Rnd))) 'Random background color
    13.   .Visible = True '[b]Do not forget this[/b]
    14. End With
    15.  
    16. End Sub
    Last edited by iPrank; Dec 5th, 2005 at 06:41 PM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  4. #4

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    Thank you both.. but i am still heavilly confused... can one of you please edit my form to use a control array so i can see it in action?

  5. #5
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help with hangman

    Add a CommandButton in your existing form (don't change name) and add my code in the form.
    Replace all Text1 in my code by txtLetter. Then run it.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  6. #6

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    ok... now there is one problem with your code... i have not learned about with statements yet...
    how can i do that same thing with a for loop?

  7. #7
    Addicted Member JensPeder's Avatar
    Join Date
    Sep 2005
    Posts
    249

    Re: help with hangman

    I just gotta ask: whats up with hangman-assignements?! Seems like every VB-class has one

  8. #8
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help with hangman

    Quote Originally Posted by coder500
    ok... now there is one problem with your code... i have not learned about with statements yet...
    how can i do that same thing with a for loop?
    Please see With Statement MSDN reference first.

    Without With the code will look like :
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.   Load Text1(Text1.UBound + 1)
    4.   Text1(Text1.UBound).Left = CInt((Me.Width + 1) * Rnd) 'Set a random Left
    5.   Text1(Text1.UBound).Top = CInt((Me.Height + 1) * Rnd) 'Set a random Top
    6.   Text1(Text1.UBound).BackColor = QBColor(CInt((15 * Rnd))) 'Random background color
    7.   Text1(Text1.UBound).Visible = True 'Do not forget this
    8.  
    9. End Sub

    Realy ugly. isn't it ?

    To do the same thing using For loop, you'll need to have a counter and loop until that counter reaches it's limit. All your array-element-adding code will be inside For loop.

    Also see this nice tutorial in VBExplorer : Creating Controls At Runtime
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: help with hangman

    With statements make my code very readable and all. But awhile back, I also did a performance test on it, and it executes a little slower using With's than without using them.

  10. #10

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    iprank, i am having problems getting that to work... the letter boxes should all be showing up next to eachother...

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    In my post, I actualy modified your code... All you needed to do was to replace it in the appropriate Clicke event.

  12. #12
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: help with hangman

    coder500, my code isn't EXACTLY what you want. I have placed them randomly. Change the Left, Top properties according to your need.

    Read the links I gave you and TextBox's Left, Right property help on MSDN. You'll find the way.

    Tip : .Left of Next textbox is Previous TxtBox's .Left + Previous TxtBox's .Width
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  13. #13

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    bruce your code is a step in the right direction but it is not putting the right number of boxes for instance... i put in the word coder and it only added one box...

  14. #14
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    I'll attach the frm, just click the Start button repeatidly.
    Attached Files Attached Files

  15. #15
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    Quote Originally Posted by coder500
    bruce your code is a step in the right direction but it is not putting the right number of boxes for instance... i put in the word coder and it only added one box...
    No probs,

    Determine the length of the word (using Len()) and use that amount to generate the number of additional TextBox's...

  16. #16

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    where would i add the Len() in the code?

  17. #17
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    No offense, but I think you may want to start with something a little easier (than hangman), as this will become more complicated. You must have a solid grounding first

    However, if you must persist; you never mentioned that the number of TextBox's was
    related to the input word. Is that the case? Better yet provide a detailaled prase
    of what you want to acheive.

  18. #18

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    my problem is.. i am doing this for a class... and i dont understand anything... but i can not drop the class cause my school wont let me...

  19. #19
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    This will add a TextBox, for each charcter of a word (entered into the txtWord TextBox):
    Replace the existing Click Event........

    VB Code:
    1. Private Sub cmdInputWord_Click()
    2.     Dim i As Integer
    3.     Dim word As String
    4.     Dim guess As String
    5.     Dim intIdx As Integer
    6.     Dim intCount As Integer
    7.        
    8.     word = txtWord.Text
    9.     txtWord.Visible = False
    10.     cmdInputWord.Visible = False
    11.    
    12.     txtWord.Text = UCase(word)
    13.     'guess = txtLetter(0).Text CANGED TO:
    14.     guess = txtLetter(txtLetter.Count - 1).Text
    15.    
    16.     'word = txtLetter(0).Index < ???
    17.    
    18.     intCount = Len(txtWord.Text)
    19.    
    20.     For intIdx = 1 To intCount - 1
    21.         Load txtLetter(intIdx)
    22.         txtLetter(intIdx).Top = txtLetter(0).Top
    23.         txtLetter(intIdx).Left = txtLetter(intIdx - 1).Left + txtLetter(intIdx).Width
    24.         txtLetter(intIdx).Text = vbNullString
    25.         txtLetter(intIdx).Visible = True
    26.     Next
    27.  
    28. End Sub

  20. #20

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    thanks for that... now i need to know how to make the txtguess field to work... and show the guesses in a pic box as well as make the correct guesses show up in the correct txtbox that was created by the code above

  21. #21
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    Quote Originally Posted by coder500
    thanks for that... now i need to know how to make the txtguess field to work... and show the guesses in a pic box as well as make the correct guesses show up in the correct txtbox that was created by the code above
    Hehe, now its time for some effort on your part
    Give it a go, and if you get stuck just ask.
    put your idea of how it should work on paper (flow diagram) and then try it with code.

  22. #22
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    Hint: The current 'word' variable need to be Public to that Form, so you can check against it for the words (letters) as they get entered

  23. #23

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    bruce thanks for all your help... i just dropped you a pm... it may be easier if we discuss this on MSN...if you have msn please pm me your address so we can talk... thanks

  24. #24
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    PM received. Don't have MSN here unfortunatly. However...... do a search on this Forum for HangMan as there a few examples, that may give you some ideas/methods etc.

    Just outa curiosity, if you are doing this for school, why would you guys be given a task that is beyond the current competency level (not singling you out mind you).

  25. #25

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    bruce...we are on chapter 7 of the book... and this is not beond the current competency level... the problem is... I dont understand this stuff easily and as a result I am not about to do the assignments without getting help.. but my friend who usually helps me has been really busy lately with college and work so i can not get his help on this one... anyways.. i will look at some other threads.. and see what i comeup with...

    ill post in a little while with my ideas... thanks again for all your help

  26. #26
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    Here (attached) is a version I did once when I was bored a while back in excell (using VBA) - may give you some ideas.
    Attached Files Attached Files

  27. #27

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    that version you posted wont run on my pc cause i have macros disabled...but... i think this thread will help me...

    i know that i need to have counters for the wrong guesses and the correct guesses... as well as have that word stored... now i just need to figure out how to make the stuff print in the location i mentioned...if correct... the guess should show up in a picDisplay and the correct location in the txt fields... if incorrect then it should show up in a picDisplay and show the next body part of the man being hung...

    give me 30 or so minutes to try to code this... and then ill post again..

    thanks

  28. #28

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    VB Code:
    1. Private Sub cmdGuess_Click()
    2. Dim word As String
    3. Dim Guess As String
    4. Dim intRight As Integer
    5. Dim intWrong As Integer
    6.  
    7. word = txtWord.Text
    8. intWrong = 0
    9. intRight = 0
    10.  
    11. Do
    12. Guess = txtGuess.Text
    13. If Len(Guess) <> 1 Then
    14. MsgBox "You must enter one letter at a time!"
    15.  
    16. ElseIf InStr(word, Guess) = 0 Then
    17. intWrong = intWrong + 1
    18. lblWrong.Text = lblWrong.Text & strGuess
    19. ElseIf InStr(strAnswer, strGuess) > 0 Then
    20. intRight = intRight + 1
    21. lblRight.Text = lblRight.Text & strGuess
    22. End If
    23.  
    24. If intWrong = 1 Then
    25. PicHead.Visible = True
    26. ElseIf intWrong = 2 Then
    27. PicBody.Visible = True
    28. ElseIf intWrong = 3 Then
    29. PicLeftArm.Visible = True
    30. ElseIf intWrong = 4 Then
    31. PicRightArm.Visible = True
    32. ElseIf intWrong = 5 Then
    33. PicLeftLeg.Visible = True
    34. ElseIf intWrong = 6 Then
    35. PicRightLeg.Visible = True
    36. MsgBox "You Lose!"
    37. End If
    38.  
    39. If intRight = Len(word) Then
    40. MsgBox "You Win!"
    41. End If
    42.  
    43. Loop While intWrong < 6 And intRight < Len(word)
    44. End Sub

    this is what i have... but i cant get it to work...

  29. #29

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    soft little bump

  30. #30

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    another soft little bump

  31. #31
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: help with hangman

    Where is the code failing?

  32. #32

    Thread Starter
    Registered User
    Join Date
    Dec 2005
    Location
    In my bed crying... because I dont understand VB
    Posts
    15

    Re: help with hangman

    bruce... thanks again for all your amazing help... I was able to get that code working... and then my friend was able to find time to help me finish the rest of the stuff up.. thank you again for your time..

  33. #33
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: [RESOLVED] help with hangman

    No problem

    Good Luck with it all.

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