From the Toolbox, add 2 Buttons, 4 Labels and a TextBox to the form.
Rename Button1 'cmdPlay'. Change the text on cmdPlay with Appearance > Text.
Rename Button2 'cmdReplay'. Change the text on cmdReplay to 'Play Again'. Make the button invisible by changing its Behavior > Visible property to False.
Change the text on Label1 with Appearance > Text.
Change the Design > Name of the TextBox to 'txtGuess' (it's more descriptive). Move txtGuess right below Label1.
Change the Design > Name of Label2 to 'lblResponse' (it's more descriptive). Move lblResponse below the button. Put something simple like "*" into the Text field of lblResponse. In the Properties window, set the Behavior > Visible property to False. This will cause this Label to be invisible when the program starts.
Change the Name of Label3 to 'lblGuesses'. Move lblGuesses below lblResponse, and put something simple in its Text field. Sets its Visible property to False.
Position Label4 above lblGuesses, and set its Text to 'Guesses:'. Set its Visible property to False.
This is what my form looks like at this point:
Double-click on the button to enter the Code View.
Write this as the first line, above Public Class Form 1: Option Explicit On.
On the line below Public Class Form 1, declare the variable 'number'. You can declare 'number' like this: Dim number As Integer. Note the dropdown menu near the top of the screen, on the left; it should read 'Form'. The similar dropdown on the right hand side should read 'Declarations'.
From the left dropdown menu, select Form1 Events. From the right dropdown menu, select Load. The code in this section will run as the form is loading, before the user has a chance to interact with the form. In Form Load, enter the command 'Randomize'. Then use the RND function to generate a random number between 1 and 100, and assign it to 'number'. Click here for more information on math functions in VB. At this link, you can click on Randomize and RND to see how they work. If, while writing and debugging your program, you want 'number' to pop up on the screen, try: MsgBox(number).
In Private Sub cmdPlay_Click, set the Visible property of lblResponse, lblGuesses and Label4 to True.
Still in cmdPlay_Click, write IF statements that respond to the user's guess. There are 3 possibilities: 'number' is either too low, too high, or correct. Let the user know about her guess by modifying lblResponse.
To extract the contents of a TextBox, access its Text property: txtGuess.Text. Use the command CInt() [short for 'Convert to Integer'] to convert the contents of txtGuess to an Integer.
The first line of the IF statement can look like this: If number > CInt(txtGuess.Text) Then. The second line can look like this: lblResponse.Text = "You are too low."
When the user guesses the number, make the button cmdReplay visible, and make the button cmdPlay invisible.
Keep track of how many times the player has guessed. Declare an Integer 'guesses' for this. Each time the player makes a guess, increment (make bigger by 1) 'guesses'. Assign 'guesses' to the Label 'lblGuesses'.
Add these lines of code to cmdPlay_Click:
This code selects the number in 'txtGuess' after the user makes a guess. Because the number is selected, the user can then make another guess without having to first delete his current guess.
In Design View, double-click on the "Play Again" button. If the user wants to play again:
- generate a new random number and assign it to 'number'
- set 'guesses' back to 0
- make the button cmdReplay invisible
- make the button cmdPlay visible, and
- set txtGuess.Text equal to " ".
Add this code to your program:
Private Sub txtGuess_KeyDown(sender As Object, e As KeyEventArgs) Handles txtGuess.KeyDown
If e.KeyCode = 13 Then
Call cmdPlay_Click(sender, e)
This code allows the user to hit the Enter key to submit a guess (clicking the button also works, of course). The Enter key has a KeyCode equal to 13.
If you want the form to fill the screen, try adjusting Layout > Size for the form.