DOCDGA
asked on
Guessing game Visual Basic 2008
I have multiple questions. How do I make my reset button populate a new random number every time it is pressed.
I also need to tweak my help message. I would like each sentence on a new line.
When user enters a blank guess I want the current message to appear but a second message appears also. How do make just first message appear and allow the user to input a correct value.
I also need to tweak my help message. I would like each sentence on a new line.
When user enters a blank guess I want the current message to appear but a second message appears also. How do make just first message appear and allow the user to input a correct value.
You want us to guess your code as well?
For random numbers:
Dim Rand as new Random
NewValue = Rand.Next(1, 100)
For multiline messages
Messagebox.Show "First Line" & VBCRLF & "Second Line"
Dim Rand as new Random
NewValue = Rand.Next(1, 100)
For multiline messages
Messagebox.Show "First Line" & VBCRLF & "Second Line"
>When user enters a blank guess I want the current message to appear but a second message appears also. How do make just first message appear and allow the user to input a correct value.
Show the relevant code.
Show the relevant code.
ASKER
'Blank guesses are not acceptable
'Remind user of valid range
If GuessTextBox.Text = "" Then
MsgBox("Blank entries not valid, input a value between 1-900.")
GuessTextBox.Text = CStr(0)
End If
If (CInt(GuessTextBox.Text) < 1 Or CInt(GuessTextBox.Text) > 900) Then
MsgBox("< 1 is not a valid guess or > 900 is not valid guess")
GuessTextBox.Text = "" 'Clear TextBox
Else
'Remind user of valid range
If GuessTextBox.Text = "" Then
MsgBox("Blank entries not valid, input a value between 1-900.")
GuessTextBox.Text = CStr(0)
End If
If (CInt(GuessTextBox.Text) < 1 Or CInt(GuessTextBox.Text) > 900) Then
MsgBox("< 1 is not a valid guess or > 900 is not valid guess")
GuessTextBox.Text = "" 'Clear TextBox
Else
'Blank guesses are not acceptable
'Remind user of valid range
If GuessTextBox.Text = "" Then
MsgBox("Blank entries not valid, input a value between 1-900.")
GuessTextBox.Text = CStr(0)
Exit Sub
End If
If (CInt(GuessTextBox.Text) < 1 Or CInt(GuessTextBox.Text) > 900) Then
MsgBox("< 1 is not a valid guess or > 900 is not valid guess")
GuessTextBox.Text = "" 'Clear TextBox
Exit Sub
Else
'Remind user of valid range
If GuessTextBox.Text = "" Then
MsgBox("Blank entries not valid, input a value between 1-900.")
GuessTextBox.Text = CStr(0)
Exit Sub
End If
If (CInt(GuessTextBox.Text) < 1 Or CInt(GuessTextBox.Text) > 900) Then
MsgBox("< 1 is not a valid guess or > 900 is not valid guess")
GuessTextBox.Text = "" 'Clear TextBox
Exit Sub
Else
ASKER
I need to use a textbox instead of a listbox. Can you help?
Option Strict On
Public Class Guess
Public number As Integer 'Holds value of random number
Public guessCounter As Integer = 0 'Tracks guesses
Public guessPercent As Double 'Guess bar tracker
' Creates a single help menu item
' A message box displays valuable & meaningful information for program & usage
Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
Dim mystring1 As String = "The program generates a random number and requires the user to input a guess between 1-900."
Dim mystring2 As String = "The user has a 5 guess limit and the game monitors the users progress."
Dim mystring3 As String = "The user can view the random number by pressing the show button."
Dim mystring4 As String = "The user can exit the game by pressing the exit button."
MsgBox(mystring1 + Environment.NewLine + mystring2 + Environment.NewLine + mystring3 + Environment.NewLine + mystring4)
End Sub
Private Sub Robin_Williams_P3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rndnumber As Random
rndnumber = New Random ' Creates an instance of random
number = rndnumber.Next(1, 900) ' Sets range for variable (1-900)
Form1.Show()
End Sub
Private Sub ShowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowButton.Click
txtGuessNumber.Text = CStr(number) 'Compare input number with random number
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close() ' Close program
End Sub
Private Sub ResetButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetButton.Click
' Generates a new random number
Dim rndnumber As Random
rndnumber = New Random ' Creates an instance of random
number = rndnumber.Next(1, 900) ' Sets range for variable (1-900)
' Enables guess textbox
GuessTextBox.Enabled = True
' Clears text content
GuessTextBox.Text = ""
' Form color reset
Me.BackColor = Color.White
' Show number label cleared
ShowMeLabel.Text = ""
' Show number button is enabled
ShowButton.Enabled = True
' Resets counter to 0
guessCounter = 0
'Reset percentage of guess bar usage
lblPercent.Text = ""
'Clears guess history list
guessNumberListBox.Items.Clear()
'Reset guess bar to 0
GuessBar.Value = 0
'Clear current random number label
txtGuessNumber.Text = ""
'Clear high or low status label
lblStatus.Text = ""
'Clear current guess
lblGuessCount.Text = ""
End Sub
Private Sub GuessButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessButton.Click
'ProgressBar settings based on guess counter
'Max out at 5 because of 5 guess limit
GuessBar.Maximum = 5
GuessBar.Minimum = 0
GuessBar.Step = 1
'Blank guesses are not acceptable
'Remind user of valid range
If GuessTextBox.Text = "" Then
MsgBox("Blank entries not valid, input a value between 1-900.")
GuessTextBox.Text = CStr(0)
End If
If (CInt(GuessTextBox.Text) < 1 Or CInt(GuessTextBox.Text) > 900) Then
MsgBox("< 1 is not a valid guess or > 900 is not valid guess")
GuessTextBox.Text = "" 'Clear TextBox
Else
'Track number of guesses
guessNumberListBox.Items.Add(GuessTextBox.Text)
TextBox1.Text = GuessTextBox.Text
' Display message if user guesses correctly
If GuessTextBox.Text = CStr(number).ToString Then
GuessTextBox.Enabled = False ' Disables textbox
Me.BackColor = Color.Red ' Background color changes red
MsgBox("You've guessed correctly!!!")
Else
' Guessed too low
If (CInt(GuessTextBox.Text) < number) Then
lblStatus.Text = "Current guess is too low. Please guess again."
GuessTextBox.Text = ""
Else
If (CInt(GuessTextBox.Text) > number) Then ' Guess too high
lblStatus.Text = "Current guess is too high. Please guess again."
GuessTextBox.Text = ""
End If
End If
End If
'Counter increments by 1
guessCounter = guessCounter + 1
'Evaluate counter
'5 guess limit
If (guessCounter = 5) Then
GuessTextBox.Enabled = False
End If
'Synchronize progress bar with guess counter
GuessBar.Value = guessCounter
guessPercent = (guessCounter / 5) * 100
lblPercent.Text = CStr(guessPercent)
'Assigns counter value to guess count label
lblGuessCount.Text = CStr(guessCounter)
End If
End Sub
End Class
What is that about? Have you got the first two questions right?
ASKER
Yeah, got the first two. Just need this part.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help