Public Class Form1
Dim randomGenerator As New Random
Dim number As Integer
Dim correctcounter As Integer = 0
Dim totalcounter As Integer = 0
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub CheckGuessBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckGuessBtn.Click
'declare variables
Dim guess As Integer
Dim excheck As Exception
Dim strErrorMsg As String
'local variable initialization
excheck = Nothing
strErrorMsg = ""
guess = 0
Try
'check if its a integer in guessTextBox
If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
'trap the error
strErrorMsg = "Enter a number."
Me.guessTextBox.Text = ""
Me.guessTextBox.Focus()
End If
'assign random number
Integer.TryParse(Me.guessTextBox.Text, guess)
If guess = number Then
correctcounter += 1
MsgBox("Congratulations! You guessed correctly.")
Else
totalcounter += 1
If totalcounter < 5 Then
MessageBox.Show("Guess another number.")
Else
MessageBox.Show("You didn't guess correctly. Want to try again?")
GetRandomNum()
End If
End If
'reset the text box
guessTextBox.Text = ""
guessTextBox.Focus()
Catch excheck
If strErrorMsg.Length = 0 Then
MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation)
End If
End Try
'clean up variables
excheck = Nothing
strErrorMsg = Nothing
guess = Nothing
End Sub
Sub GetRandomNum()
number = randomGenerator.Next(1, 6)
Debug.Print("Number is {0}", number) 'test
totalcounter = 0
End Sub
Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
MsgBox("A new number has been selected for you to guess.")
GetRandomNum()
'initialize the random number.
Dim randomgenerator As New Random
number = randomgenerator.Next(1, 6)
totalcounter = 0
'gamecounter += 1
guessTextBox.Focus()
End Sub
Private Sub guessTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles guessTextBox.Enter
'selects the existing text
Me.guessTextBox.SelectAll()
End Sub
Private Sub guessTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guessTextBox.KeyPress
'allows only numbers and the backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
I'm trying to get the program to assign a random number for someone to guess. They get five chances, numbers 1-5. However, it always says Incorrect number, but when I click on Check without putting a number in, its says "Congratulations." Obviously I'm doing something wrong.
Also, I need to add an IF/THEN statement for someone to guess either a higher or lower number, but not sure how to do that. Any suggestions?
southerngenes, on 27 Jun, 2009 - 09:26 AM, said:
Public Class Form1
Dim randomGenerator As New Random
Dim number As Integer
Dim correctcounter As Integer = 0
Dim totalcounter As Integer = 0
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub CheckGuessBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckGuessBtn.Click
'declare variables
Dim guess As Integer
Dim excheck As Exception
Dim strErrorMsg As String
'local variable initialization
excheck = Nothing
strErrorMsg = ""
guess = 0
Try
'check if its a integer in guessTextBox
If Integer.TryParse(Me.guessTextBox.Text, guess) = False Then
'trap the error
strErrorMsg = "Enter a number."
Me.guessTextBox.Text = ""
Me.guessTextBox.Focus()
End If
'assign random number
Integer.TryParse(Me.guessTextBox.Text, guess)
If guess = number Then
correctcounter += 1
MsgBox("Congratulations! You guessed correctly.")
Else
totalcounter += 1
If totalcounter < 5 Then
MessageBox.Show("Guess another number.")
Else
MessageBox.Show("You didn't guess correctly. Want to try again?")
GetRandomNum()
End If
End If
'reset the text box
guessTextBox.Text = ""
guessTextBox.Focus()
Catch excheck
If strErrorMsg.Length = 0 Then
MessageBox.Show(strErrorMsg, "Error", MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation)
End If
End Try
'clean up variables
excheck = Nothing
strErrorMsg = Nothing
guess = Nothing
End Sub
Sub GetRandomNum()
number = randomGenerator.Next(1, 6)
Debug.Print("Number is {0}", number) 'test
totalcounter = 0
End Sub
Private Sub newGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newGame.Click
MsgBox("A new number has been selected for you to guess.")
GetRandomNum()
'initialize the random number.
Dim randomgenerator As New Random
number = randomgenerator.Next(1, 6)
totalcounter = 0
'gamecounter += 1
guessTextBox.Focus()
End Sub
Private Sub guessTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles guessTextBox.Enter
'selects the existing text
Me.guessTextBox.SelectAll()
End Sub
Private Sub guessTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guessTextBox.KeyPress
'allows only numbers and the backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub

New Topic/Question
Reply




MultiQuote






|