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

totalRolls = txtTotalRolls.text

This takes the value the user puts in the text box and stores it in the variable totalRolls

A For Next loop looks a bit like this:

For Counter = 1 to 5

       Eat lolly

Next Counter

The Counter is a variable that counts up from 1 to 5. The first time through the loop, counter has the value 1, the second time through counter has the value 2,  etc.  After the fifth time through, counter has the value 5, so a total of 5 lollies will be eaten.  

So our code for the loop will be:

For counter = 1 to totalRolls

Roll Die and add to total

Next counter

Comment lines make it easier for ourselves and other programmers to understand the code. They make finding errors easier.

Comment lines start with an apostrophe ' and are ignored by the computer.  You can also add comments to the end of another line.  Everything after the apostrophe will be ignored.

' this is a comment

 

Private Sub imgDie_Click()

Dim totalRolls, counter, dieRoll, totalOnes, totalTwos, totalThrees, totalFours, totalFives, totalSixes  as Long

Randomize

totalOnes=0: totalTwos=0: totalThrees=0: totalFours=0:totalFives=0: totalSixes=0 'initialise total counters

totalRolls = txtTotalRolls.text  'get the total number of rolls

For counter = 1 to totalRolls

DieRoll = Int (Rnd*6)+1    'roll die

Select Case DieRoll

Case 1
      totalOnes=totalOnes+1

Case 2
      totalTwos=totalTwos +1

Case 3
      totalThrees=totalThrees +1

Case 4
      totalFours=totalFours +1

Case 5
      totalFives=totalFives+1

Case 6
      totalSixes=totalSixes+1

End Select

Next counter

'output the totals for each number

lblOnes.caption=totalOnes
lblTwos.caption=totalTwos
lblThrees.caption=totalThrees
lblFours.caption=totalFours
lblFives.caption=totalFives
lblSixes.caption=totalSixes

End Sub

 

 


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!)

You may notice that the program can crash if the user puts in something other than a number into the text box.  Can you work out a way to solve this problem?