Double click on the button. We are going to add the code that gets the program to do something when the user hits the button to start the game. We double click on the item because that is the way to switch to the part of visual basic that lets us write the code that makes stuff happen as opposed to the part of visual basic that lets us add and move things around on the screen.
Brings up the code. Notice the GoButton_Click. In between the Private Sub GoButton_Click and the End Sub is where we will write our code because we are specifically doing things when someone clicks the button.
Our overall application is Form1. Remember that we did not change the name of the window which was called Form1. All we did was to change the name on the window which is a text property of the window. Notice that the name of our code is called Form1.vb. If we change the name of our window, we will end up changing the name of all that other stuff as well. The Form1.vb is our code. The Form1.vb (Design) is where you can manipulate the items on our application window.
Public Class Form1 Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click ' Declare the variables Dim Dice1 As Integer Dim Dice2 As Integer ' Produce the random dice rolls Randomize() Dice1 = CInt(Int(6 * Rnd()) + 1) Dice2 = CInt(Int(6 * Rnd()) + 1) ' Display the result DiceRoll1.Text = Dice1 DiceRoll2.Text = Dice2 End Sub End ClassThere are three main parts to our code so far. Remember that the quote mark at the start of a line is a comment, which is only for the benefit of the programmer reading the code and has no effect on what happens.
The first section is Declare the variables which will identify the buckets of data (which are called variables) that are going to be generated by this program. We need to keep track of each roll of the dice. Many people will be confused by this code because we already set aside a place to display the results of the dice rolls in the user interface. There is a difference between the user interface text box that we use to display the roll and here in the code. In the code, we need to set aside a variable for the dice roll so that we can manipulate it before we display it. It is also the case that we may have many other variables in our program; more than what we display to the user.
The variables are named Dice1 and Dice2. The Dim is used to say that we want to set aside some space for these variables, and it is shorthand for the word Dimension because we are going to say what the dimensions (size) of the variable need to be. The type (in this case Integer) of the variable will let the computer know what sorts of things we are going to store in our variable. An Integer tells the computer we are going to store only whole numbers (such as 1, 100, or 3234). We are going to only store a number that is between 1 and 6 which is why an Integer was chosen. If we wanted to store numbers that had a decimal place, such as a dollar amount (like 1.34) we would use a Double. There are many types of variables. The word As just comes between the variable and its type.
Dim Dice1 As Integer
Dim Dice2 As Integer
The second section is Produce the random dice rolls which produces a random number between 1 and 6 for each roll. The statement Randomize() is used to get the computer to come up with a more random random number.
We are going to assign usign the equal sign the result of what looks like some complicated math. We need to work our way from the inside out. The Rnd() will produce a random number between 0 and 1. A number like 0.12345 is a possible result. In order to make that number in the range that we need it, we need to multiply the Rnd() number by 6 because we want 6 possible values. The result of that will produce a number between 0 and 5. We add 1 to that value to get between 1 and 6. Actually we do not get whole numbers because the Rnd() gave us a number with decimal places, so we use the Int to convert the Double into an Int.
Dice1 = CInt(Int(6 * Rnd()) + 1)
Dice2 = CInt(Int(6 * Rnd()) + 1)
The third section is Display the result which then will display the value in the variable to the user interface. Remember that there is a difference between our variables which we manipulate in our program and the place in which we display values to the user.
DiceRoll1.Text = Dice1
DiceRoll2.Text = Dice2
Run the program. Press the Go button and you will get the two rolls displayed. Press it again and you will get two other numbers to show up. Every time you press the Go button you are running the code that we wrote. Remember that the code we wrote is based on the click of the button.