Starting and Initializing the Math Game Program

The Click() event procedure of the cmdBegin Command Button control serves as the main procedure in the Math Game program. This procedure initializes a few variables, clears the worksheet, and makes several calls to sub procedures that get the game started.

Private Sub cmdBegin_Click()

'Initialize variables and controls.

EnableControls False numQuestions = 0

gameRunning = True

Range("A2:C" & UsedRange.Rows.Count).ClearContents Range("Answer").Select Application.MoveAfterReturn = False

'Get the operator type and operands for the question.

GetOperatorType GetOperands

'Mark the start time and start the clock.

curDate = Now MathGame End Sub

Examination of the Click() event procedure of the Command Button control cmdBegin shows an immediate call to the sub procedure EnableControls(). This procedure is used to enable or disable the ActiveX controls on the worksheet via a Boolean value (passed in to the parameter ctrlsEnabled, see next sub procedure). At this stage of the program, I want to disable all ActiveX controls so the user doesn't accidentally select one while the game runs; therefore, I pass in the value false.

After the ActiveX controls are disabled, a couple more module-level variables are initialized (numQuestions and gameRunning) before the first three columns of the spreadsheet are cleared. The UsedRange property of the Application object returns exactly what its name implies—the range on the worksheet containing the data. I use this range along with the Rows and Count properties of the Range object to tell me how many rows are used on the spreadsheet so they can be cleared (see Chapter 5 for a discussion of the Application object, Range object, and their properties). This effectively clears the results of a previous game from the worksheet.

After clearing the worksheet of the previous game's results, the range of cells in which the user must enter his/her answer is selected. This is the range L8:M9 which I merged and defined a name for (Answer) in the Excel application. The MoveAfterReturn property of the Application object is set to false to prevent the cursor from moving (usually down one cell) after the user presses enter on the keyboard. This feature can be found in the Excel application under Tools, Options, and the Edit tab (see Figure 4.15).

I only had a vague memory of setting the cursor direction after Enter feature in Excel, so I searched Options dialog (because it seemed like the most reasonable place) in the Excel application until I found it. Then, with the macro recorder turned on, I deselected the Move selection after Enter check box (see Figure 4.15) and examined the resulting VBA code to learn how to program this feature.

Move selection after Enter

The Edit tab of the Options dialog in the Excel application.

Move selection after Enter

The Edit tab of the Options dialog in the Excel application.

Private Sub EnableControls(ctrlsEnabled As Boolean) 'Enables/Disables ActiveX controls on the worksheet. cmdBegin.Enabled = ctrlsEnabled optAdd.Enabled = ctrlsEnabled optSubtract.Enabled = ctrlsEnabled optDivide.Enabled = ctrlsEnabled optMultiply.Enabled = ctrlsEnabled optAny.Enabled = ctrlsEnabled End Sub

After the game's variables and controls are initialized, the first question is randomly generated before starting the timer.

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