yaahta 12-23-2005, 10:25 PM I know this is beneath most of yall, but can I get some help making my Hangman program work.
The problem is that on the main interface it won't show the secret word, the incorrect letter guesses or the incorrect guesses when I call it up. However it will go through the motions and display the information I coded for the input and message boxes. Here is what I have so far..
'declare form-level variable
Private Sub ProcessLetterLabels(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ALabel.Click, BLabel.Click, CLabel.Click, DLabel.Click, ELabel.Click, _
FLabel.Click, GLabel.Click, HLabel.Click, ILabel.Click, JLabel.Click, _
KLabel.Click, LLabel.Click, MLabel.Click, NLabel.Click, OLabel.Click, _
PLabel.Click, QLabel.Click, RLabel.Click, SLabel.Click, TLabel.Click, _
ULabel.Click, VLabel.Click, WLabel.Click, XLabel.Click, YLabel.Click, ZLabel.Click
End Sub
Private Sub FileExitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileExitMenuItem.Click
'ends the application
Me.Close()
End Sub
Private Sub FileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileNewMenuItem.Click
'simulates the hangman game
Dim word As String 'stores the word to be guessed
Dim letter As String 'stores the letter guesses
Dim dashReplaced As Boolean 'indicates whether a dash was replaced
Dim gameOver As Boolean 'indicates whether the game is over
Dim incorrectGuesses As Integer 'keeps track if the number if incorrect guesses
Dim indexNum As Integer 'controls the loop that searches the word
'get a 5-letter word from the first player
Do
word = InputBox("Enter a 5-letter word", "Hangman Game")
Loop Until word.Length = 5
'convert word to uppercase
word = word.ToUpper()
'display five dashes in WordLabel control
Me.WordLabel.Text = "-----"
'clear the label controls
Me.IncorrectGuessLabel.Text = ""
Me.IncorrectLettersLabel.Text = ""
'allow player 2 to guess a letter
'the game is over when either the word is guessed or
'player 2 makes 10 incorrect guesses
Do While Not gameOver
'get a letter from player 2
letter = InputBox("Enter a letter:", "Letter", "", 500, 500)
'convert letter to unppercase
letter = letter.ToUpper()
'search the word for the letter
For indexNum = 0 To word.Length - 1
If word.Substring(indexNum, 1) = letter Then
'replace appropriate dash in the Wordlabel
Mid(Me.WordLabel.Text, indexNum + 1) = letter
'indicate that a replacement was made
dashReplaced = True
End If
Next indexNum
'determine whether a replacement was made
If dashReplaced Then
'if the word does not contain any dashes, then
'the user guessed the word, so the game is over
If Me.WordLabel.Text.IndexOf("-") = -1 Then
gameOver = True
MessageBox.Show("Great guessing!", "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else 'reset the dashReplaced variable
dashReplaced = False
End If
Else 'processed when no dash was replashed
'display incorrect letter
Me.IncorrectLettersLabel.Text = _
Me.IncorrectLettersLabel.Text & " " & letter
'update the counter variable, then display the result
incorrectGuesses = incorrectGuesses + 1
Me.IncorrectGuessLabel.Text = Convert.ToString(incorrectGuesses)
'determine whether player 2 made 10 incorrect guesses
If incorrectGuesses = 1 Then
'the game is over
gameOver = True
Me.WordLabel.Text = "Game Over"
MessageBox.Show("Sorry, the word is " & word, "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Loop
End Sub
Iceplug 12-24-2005, 04:11 AM Wow... you're using a game loop for Hangman? The reason why you aren't seeing any changes to the interface is because you aren't giving it any time to process. Realize that the interface doesn't draw itself when there is nothing apparently going on... it only draws when it has permission to... and showing an InputBox does not allow the form to redraw because the InputBox is more important (i.e. Modally shown).
So, you need to call Application.DoEvents right before each Loop statement... this gives permission for your Application to process everything that needs to be done to other parts of it not included in this block (i.e. updating graphics displays on a window)
:)
yaahta 12-24-2005, 05:56 AM I tried using the Application.DoEvents before the Loops, but I am still not seeing the main interface. It's frustration, because it looks like it should work.
All the friends, controls and variables are accounted for. Everything is closed out... I am missing something somewhere...any suggestions???
Thanks for taking the time to help.
Wow... you're using a game loop for Hangman? The reason why you aren't seeing any changes to the interface is because you aren't giving it any time to process. Realize that the interface doesn't draw itself when there is nothing apparently going on... it only draws when it has permission to... and showing an InputBox does not allow the form to redraw because the InputBox is more important (i.e. Modally shown).
So, you need to call Application.DoEvents right before each Loop statement... this gives permission for your Application to process everything that needs to be done to other parts of it not included in this block (i.e. updating graphics displays on a window)
:)
Iceplug 12-24-2005, 06:40 AM No, not before the loops themselves, but before the actual keyword Loop...
as in the Application.DoEvents would be inside of the loop, but as the last statement of the loop. :)
Do Until something
'stuff
Application.DoEvents
Loop
yaahta 12-25-2005, 04:36 AM I put that in, but I still don't see the main interface. Maybe I am missing something I don't see. Here is the complete program in installments. Do you see anything?? I took the Application.Do events out for this display.
p1
Public Class HangmanForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents WordLabel As System.Windows.Forms.Label
Friend WithEvents VLabel As System.Windows.Forms.Label
Friend WithEvents ZLabel As System.Windows.Forms.Label
Friend WithEvents XLabel As System.Windows.Forms.Label
Friend WithEvents RLabel As System.Windows.Forms.Label
Friend WithEvents MLabel As System.Windows.Forms.Label
Friend WithEvents ILabel As System.Windows.Forms.Label
Friend WithEvents OLabel As System.Windows.Forms.Label
Friend WithEvents TLabel As System.Windows.Forms.Label
Friend WithEvents LLabel As System.Windows.Forms.Label
Friend WithEvents BLabel As System.Windows.Forms.Label
Friend WithEvents PLabel As System.Windows.Forms.Label
Friend WithEvents CLabel As System.Windows.Forms.Label
Friend WithEvents DLabel As System.Windows.Forms.Label
Friend WithEvents HLabel As System.Windows.Forms.Label
Friend WithEvents ULabel As System.Windows.Forms.Label
Friend WithEvents YLabel As System.Windows.Forms.Label
Friend WithEvents WLabel As System.Windows.Forms.Label
Friend WithEvents NLabel As System.Windows.Forms.Label
Friend WithEvents QLabel As System.Windows.Forms.Label
Friend WithEvents GLabel As System.Windows.Forms.Label
Friend WithEvents ELabel As System.Windows.Forms.Label
Friend WithEvents JLabel As System.Windows.Forms.Label
Friend WithEvents KLabel As System.Windows.Forms.Label
Friend WithEvents ALabel As System.Windows.Forms.Label
Friend WithEvents FLabel As System.Windows.Forms.Label
Friend WithEvents SLabel As System.Windows.Forms.Label
Friend WithEvents MsgLabel As System.Windows.Forms.Label
Friend WithEvents IncorrectGuessLabel As System.Windows.Forms.Label
Friend WithEvents IdIncorrectGuessLabel As System.Windows.Forms.Label
Friend WithEvents IdWordLabel As System.Windows.Forms.Label
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents IncorrectLettersLabel As System.Windows.Forms.Label
Friend WithEvents HangmanMainMenu As System.Windows.Forms.MainMenu
Friend WithEvents FileMenuTitle As System.Windows.Forms.MenuItem
Friend WithEvents FileNewMenuItem As System.Windows.Forms.MenuItem
Friend WithEvents FileExitMenuItem As System.Windows.Forms.MenuItem
Friend WithEvents FileSeparator As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SLabel = New System.Windows.Forms.Label
Me.JLabel = New System.Windows.Forms.Label
Me.FLabel = New System.Windows.Forms.Label
Me.ZLabel = New System.Windows.Forms.Label
Me.VLabel = New System.Windows.Forms.Label
Me.LLabel = New System.Windows.Forms.Label
Me.MLabel = New System.Windows.Forms.Label
Me.IncorrectGuessLabel = New System.Windows.Forms.Label
Me.KLabel = New System.Windows.Forms.Label
Me.ALabel = New System.Windows.Forms.Label
Me.IdIncorrectGuessLabel = New System.Windows.Forms.Label
Me.PLabel = New System.Windows.Forms.Label
Me.IdWordLabel = New System.Windows.Forms.Label
Me.WLabel = New System.Windows.Forms.Label
Me.OLabel = New System.Windows.Forms.Label
Me.NLabel = New System.Windows.Forms.Label
Me.YLabel = New System.Windows.Forms.Label
Me.BLabel = New System.Windows.Forms.Label
Me.WordLabel = New System.Windows.Forms.Label
Me.RLabel = New System.Windows.Forms.Label
Me.QLabel = New System.Windows.Forms.Label
Me.GLabel = New System.Windows.Forms.Label
Me.HLabel = New System.Windows.Forms.Label
Me.DLabel = New System.Windows.Forms.Label
Me.ILabel = New System.Windows.Forms.Label
Me.ELabel = New System.Windows.Forms.Label
Me.XLabel = New System.Windows.Forms.Label
Me.TLabel = New System.Windows.Forms.Label
Me.CLabel = New System.Windows.Forms.Label
Me.ULabel = New System.Windows.Forms.Label
Me.MsgLabel = New System.Windows.Forms.Label
Me.IncorrectLettersLabel = New System.Windows.Forms.Label
Me.HangmanMainMenu = New System.Windows.Forms.MainMenu
Me.FileMenuTitle = New System.Windows.Forms.MenuItem
Me.FileNewMenuItem = New System.Windows.Forms.MenuItem
Me.FileExitMenuItem = New System.Windows.Forms.MenuItem
Me.FileSeparator = New System.Windows.Forms.MenuItem
Me.SuspendLayout()
'
'SLabel
'
Me.SLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.SLabel.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.SLabel.Location = New System.Drawing.Point(144, 176)
Me.SLabel.Name = "SLabel"
Me.SLabel.Size = New System.Drawing.Size(16, 23)
Me.SLabel.TabIndex = 19
Me.SLabel.Text = "S"
Me.SLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.SLabel.Visible = False
'
'JLabel
'
Me.JLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.JLabel.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.JLabel.Location = New System.Drawing.Point(240, 144)
Me.JLabel.Name = "JLabel"
Me.JLabel.Size = New System.Drawing.Size(16, 23)
Me.JLabel.TabIndex = 10
Me.JLabel.Text = "J"
Me.JLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.JLabel.Visible = False
'
'FLabel
'
Me.FLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.FLabel.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.FLabel.Location = New System.Drawing.Point(144, 144)
Me.FLabel.Name = "FLabel"
Me.FLabel.Size = New System.Drawing.Size(16, 23)
Me.FLabel.TabIndex = 6
Me.FLabel.Text = "F"
Me.FLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.FLabel.Visible = False
'
'ZLabel
'
Me.ZLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.ZLabel.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ZLabel.Location = New System.Drawing.Point(312, 176)
Me.ZLabel.Name = "ZLabel"
Me.ZLabel.Size = New System.Drawing.Size(16, 23)
Me.ZLabel.TabIndex = 26
Me.ZLabel.Text = "Z"
Me.ZLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.ZLabel.Visible = False
'
'VLabel
'
Me.VLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.VLabel.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.VLabel.Location = New System.Drawing.Point(216, 176)
Me.VLabel.Name = "VLabel"
Me.VLabel.Size = New System.Drawing.Size(16, 23)
Me.VLabel.TabIndex = 22
Me.VLabel.Text = "V"
Me.VLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.VLabel.Visible = False
'
'LLabel
'
Me.LLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.LLabel.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LLabel.Location = New System.Drawing.Point(288, 144)
Me.LLabel.Name = "LLabel"
Me.LLabel.Size = New System.Drawing.Size(16, 23)
Me.LLabel.TabIndex = 12
Me.LLabel.Text = "L"
Me.LLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.LLabel.Visible = False
'
'MLabel
'
Me.MLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.MLabel.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.MLabel.Location = New System.Drawing.Point(312, 144)
Me.MLabel.Name = "MLabel"
Me.MLabel.Size = New System.Drawing.Size(16, 23)
Me.MLabel.TabIndex = 13
Me.MLabel.Text = "M"
Me.MLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.MLabel.Visible = False
'
yaahta 12-25-2005, 04:44 AM The rest is the code we have been discussing. Any insight would be helpful.
THANKS
Iceplug 12-25-2005, 05:55 AM I wanted to see the new loop that you made with the DoEvents. And please, if you want to post everything on your form, put it in a ZIP file or rename it to a txt temporarily and post it here... rather than posting 3 postfuls of designer code that I don't need to see. :huh:
~dizzy from all of that scrolling down~ ;)
yaahta 12-25-2005, 07:39 AM Sorry...didn't know I could attach zip files....
Here is the code with the Application.DoEvents. There are right before the two Loops. It returns a error with them in the code.
Private Sub FileNewMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileNewMenuItem.Click
'simulates the hangman game
Dim word As String 'stores the word to be guessed
Dim letter As String 'stores the letter guesses
Dim dashReplaced As Boolean 'indicates whether a dash was replaced
Dim gameOver As Boolean 'indicates whether the game is over
Dim incorrectGuesses As Integer 'keeps track if the number if incorrect guesses
Dim indexNum As Integer 'controls the loop that searches the word
'get a 5-letter word from the first player
Do
word = InputBox("Enter a 5-letter word", "Hangman Game")
Application.DoEvents()
Loop Until word.Length = 5
'convert word to uppercase
word = word.ToUpper()
'display five dashes in WordLabel control
Me.WordLabel.Text = "-----"
'clear the label controls
Me.IncorrectGuessLabel.Text = ""
Me.IncorrectLettersLabel.Text = ""
'allow player 2 to guess a letter
'the game is over when either the word is guessed or
'player 2 makes 10 incorrect guesses
Do While Not gameOver
'get a letter from player 2
letter = InputBox("Enter a letter:", "Letter", "", 500, 500)
'convert letter to unppercase
letter = letter.ToUpper()
'search the word for the letter
For indexNum = 0 To word.Length - 1
If word.Substring(indexNum, 1) = letter Then
'replace appropriate dash in the Wordlabel
Mid(Me.WordLabel.Text, indexNum + 1) = letter
'indicate that a replacement was made
dashReplaced = True
End If
Next indexNum
'determine whether a replacement was made
If dashReplaced Then
'if the word does not contain any dashes, then
'the user guessed the word, so the game is over
If Me.WordLabel.Text.IndexOf("-") = -1 Then
gameOver = True
MessageBox.Show("Great guessing!", "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else 'reset the dashReplaced variable
dashReplaced = False
End If
Else 'processed when no dash was replashed
'display incorrect letter
Me.IncorrectLettersLabel.Text = _
Me.IncorrectLettersLabel.Text & " " & letter
'update the counter variable, then display the result
incorrectGuesses = incorrectGuesses + 1
Me.IncorrectGuessLabel.Text = Convert.ToString(incorrectGuesses)
'determine whether player 2 made 10 incorrect guesses
If incorrectGuesses = 1 Then
'the game is over
gameOver = True
Me.WordLabel.Text = "Game Over"
MessageBox.Show("Sorry, the word is " & word, "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
Application.DoEvents()
Loop
End Sub
End Class
I wanted to see the new loop that you made with the DoEvents. And please, if you want to post everything on your form, put it in a ZIP file or rename it to a txt temporarily and post it here... rather than posting 3 postfuls of designer code that I don't need to see. :huh:
~dizzy from all of that scrolling down~ ;)
Iceplug 12-25-2005, 11:48 AM So, you get an error now? Which error is it?
yaahta 12-25-2005, 12:30 PM Actually, my mistake I only get the error when I only put the second .DoEvents in.
It is the error that says
An unhandled exception of type 'System.NullReferenceException' occurred in Unknown Module.
Additional information: Object reference not set to an instance of an object.
However when I put both in it goes through the motions the same way it did before and the main interface still does not show.
Iceplug 12-25-2005, 03:05 PM So, when you put both in, do you get the error or do you not see the interface? Only one should be happening... well, I suppose having an error would also cause nothing to appear on the display, because nothing is actually happening. Does the error appear on the Application.DoEvents line and is it highlighted green?
Do you have any other control events in the program besides this menu click?
yaahta 12-27-2005, 11:20 PM When I put both in I do not see the interface. The error does appear on the Application.DoEvents line and is it highlighted green. However, it only happens when I put one of the .DoEvents in, otherwise I just get nothing (no inerface). I do not have any more control events. What I put in these emails is all the code that is in my program.
So, when you put both in, do you get the error or do you not see the interface? Only one should be happening... well, I suppose having an error would also cause nothing to appear on the display, because nothing is actually happening. Does the error appear on the Application.DoEvents line and is it highlighted green?
Do you have any other control events in the program besides this menu click?
|