Add a label and a text box. Rather than belabor the steps you need to take to add a new text box and a label for it, if you need help look at earlier parts of this book for information about adding a text box and a label. You want to change the text property of the label to be Come Out so that when the user sees the value in the text box, he will know what is being displayed there.
Rename the text box to be ComeOutRoll. Remember to change the text box property in order to rename the text box using the (name) property. Now that we begin to have several text boxes, it is important that we name them appropriately as we will be putting values into the text boxes. You can change the (name) of the label as well, but since we are not going to modify their contents in our code it was not necessary to change them. It is a good habit to change even the labels, but you can do that on your own.
The following is how we need to modify our code in order to produce and display the come out roll.
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 Dim ComeOut 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 ' Compute the come out roll ComeOut = Dice1 + Dice2 ' Display the come out roll ComeOutRoll.Text = ComeOut End Sub End Class
There are two more sections of code added to our program.
The fourth section, which is the first section added, is Compute the come out roll which uses the result of our other two variables which we add together to calculate our come out roll. We will be using the come out roll later on in our program.
ComeOut = Dice1 + Dice2
The fifth section, which is the section section added, is Display the come out roll which assigns the value of the ComeOut variable to the text property of the ComeOutRoll text box. It is not an accident that the variable and the text box were named differently. You cannot have two variables with the same name, nor two user interface items, nor a variable and a user interface item. They must all be unique so that the computer knows which one you are talking about.
ComeOutRoll.Text = ComeOut
Run the program. This time when you hit Go, you will see displayed the two rolls, and also the sum of the two rolls together. Each time you hit Go you will get the rolls added up as well. While we do not need to display the rolls to the user, it makes for a better experience. At the same time, we do not need to display the sum of the two rolls together as a come out value, but the user would prefer that over having to do it on his own.