We are going to modify the previous die rolling application so that it rolls the die a chosen number of times and keeps a tally of how many of each number has been rolled. The user will click on the die image to start rolling.
Design the interface
Draw your interface like this:

Name the text box txtTotalRolls, set its text to be 0 (Make sure it is big enough for 6 digits)
Name the pink coloured labels lblOnes, lblTwos, lblThrees etc These will show the total of each number rolled. Set their captions to be 0. (Make sure that they are wide enough to take 6 digits.
Name the picture imgDie. We will click it to start.
Name the exit button cmdExit.
Write the Code
-
We will set up nine variables called totalRolls, counter, dieRoll, totalOnes, totalTwos, totalThrees, totalFours, totalFives, and totalSixes to store the totals.
-
At the start of the program, we will set the value of most of the variables to 0 (this is called initialising them) totalOnes=0: totalTwos=0 etc (The colon : can be used to put more than one line of code on the same line)
-
To get the total number of rolls that the user enters, we will use the line:
totalRolls = txtTotalRolls.text
This takes the value the user puts in the text box and stores it in the variable totalRolls
-
We will use a For...Next loop to repeat the die rolling the required number of times.
|
|
|
So our code for the loop will be:
For counter = 1 to totalRolls
Roll Die and add to total
Next counter
-
To keep count of how many of each number there have been, we will use this code:
totalOnes = totalOnes + 1
(totalOnes is assigned the value totalOnes plus 1)
This means take whatever value is stored in the variable totalOnes, add 1 to it, then store the value back in the variable totalOnes.
-
Because our code is getting complicated, so we will use comments explaining it as we go.
|
|
|
-
So our code for when the die picture is clicked will be as follows:
|
Private Sub imgDie_Click()
End Sub |
- Attach code for when the exit button is clicked
Test your Application
Test your application, taking careful note of any problems, especially with regard to label sizes. You may need to adjust font sizes or make labels autosize (watch out they don't autosize over top of each other!)
![]() |
|


