Resetting the Game Board

Before a user can play a game of Poker Dice, he or she must reset the game board by clearing the dice, check marks, and text. I handle the resetting of the game board with the procedures ToggleControls() and the Click() event of the Command Button control named cmdNewGame. These procedures are fairly straightforward.

The ToggleControls() sub procedure is passed a Boolean parameter that is used to enable or disable all of the Check Box and Image controls on the game board. Set the Enabled property of an ActiveX control to true in order to activate the control for use. Set the Enabled property of an ActiveX control to false to make it unavailable to the user. Please note: the caption will be grayed out. These controls must be disabled prior to the first roll of the dice to prevent the user from accidentally selecting one of these controls. If a Check Box is selected prior to the first roll, the dice's image will not be loaded for its corresponding Image control (you will see why shortly). The ToggleControls() sub procedure must be called later in the program so that the Check Box and Image controls are enabled prior to the user making his or her second roll of the dice. Note that one Boolean value must be passed to the ToggleControls() sub procedure in order to specify enabling or disabling the controls. The procedure's scope is private since it only needs to be accessed from code in the object module.

The code in the ToggleControls() sub procedure could have been Left in the Click() event procedure of the Command Button control; however, moving this code to a custom sub procedure serves to shorten the Click() event procedure, and prevents a code redundancy later in the program for re-enabling these controls.

Private Sub ToggleControls(toggle As Boolean) 'Toggle the Enabled property of the Check Box and Image controls. ckBoxl.Enabled = toggle ckBox2.Enabled = toggle ckBox3.Enabled = toggle

ckBox4.Enabled = toggle ckBox5.Enabled = toggle

mgDicel.

Enabled

= toggle

mgDice2.

Enabled

= toggle

mgDice3.

Enabled

= toggle

mgDice4.

Enabled

= toggle

mgDice5.

Enabled

The Click() event of the Command Button control named cmdNewGame clears the Check Boxes, images, and text from the game board and calls the ToggleControls() sub procedure. You can remove any checks selected by the user by setting the Value property of all Check Box controls to false. To clear a cell's content you can set the Value property of the cell to an empty string as I have done with the merged cells on the game board. Note that when referring to cells that have been merged, use the row and column indices of the upper left cell in the merged group, in this case cell C12. To allow the user his or her first roll, you must enable and disable the Command Buttons cmdRollDice and cmdNewGame, respectively. Finally, you can remove the images from the Image controls by passing an empty string to VBA's LoadPicture() function.

Private Sub cmdNewGame_Click()

'Initialize ActiveX controls on the worksheet.

'Clear check box controls.

ckBoxl.Value = False ckBox2.Value = False ckBox3.Value = False ckBox4.Value = False ckBox5.Value = False

ToggleControls False 'Call sub to disable Image and Check Box controls

'Clear text from merged cells. Enable/disable buttons. 'Clear images from Image controls.

Range("C12").Value = "" cmdRollDice.Enabled = True cmdNewGame.Enabled = False imgDicel.Picture = LoadPicture( imgDice2.Picture imgDice3.Picture imgDice4.Picture imgDice5.Picture End Sub

LoadPicture( LoadPicture( LoadPicture( LoadPicture(

Figure 3.11 shows the Poker Dice game board while the user attempts to click on an Image control after it has been reset. The Image control is grayed while the user clicks it, but its associated Check Box is not and cannot be checked at this time.

The Poker Dice game board after resetting.

Figure 3.11 shows the Poker Dice game board while the user attempts to click on an Image control after it has been reset. The Image control is grayed while the user clicks it, but its associated Check Box is not and cannot be checked at this time.

The Poker Dice game board after resetting.

Biorhythm Awareness

Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook


Post a comment