19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

296 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

When a check box has the focus, the spacebar can be used to check (or uncheck) the box<br />

<strong>and</strong> invoke the Click event. In addition, the state of a check box can be toggled from the keyboard<br />

without first setting the focus to the check box if you create an access key for the check<br />

box by including an ampers<strong>and</strong> in the Caption property. (Access keys appear underlined at<br />

run time.) For instance, if the Caption property for the Dental Plan in Example 1 is set as<br />

“&Dental Plan”, then the user can check (or uncheck) the box by pressing Alt+D.<br />

Notice that the program code for the solution to Example 1 involved four identical click<br />

event procedures. This is a good indication that a control array of check boxes will simplify<br />

the program, as shown in Example 2.<br />

EXAMPLE 2<br />

The following program reworks Example 1 using a control array of check boxes, with an access key for<br />

each check box. The program has been made more general <strong>and</strong> easy to update by placing the name <strong>and</strong><br />

cost of each benefit plan in the data file BENEFITS.TXT. Each line of the file consists of the name of the<br />

plan followed by the cost of the plan, as illustrated by the first line of the file:<br />

“&Prescription Drug Plan”, 12.51<br />

Object Property Setting<br />

frmBenefits Caption Benefits Menu<br />

chkPlan( ) Index 0 through 3<br />

lblTotal Caption Total monthly payment:<br />

lblAmount Caption $0.00<br />

Dim price(0 To 3) As Single ‘In (Declarations) section of (General)<br />

Dim sum As Single<br />

Private Sub Form_Load()<br />

Dim i As Integer, plan As String, cost As Single<br />

Open “BENEFITS.TXT” For Input As #1<br />

For i = 0 To 3<br />

Input #1, plan, cost<br />

price(i) = cost<br />

chkPlan(i).Caption = plan & “ (” & FormatCurrency(cost) & “)”<br />

Next i<br />

Close 1<br />

sum = 0<br />

End Sub<br />

Private Sub chkPlan_Click(Index As Integer)<br />

If chkPlan(Index).Value = 1 Then<br />

sum = sum + price(Index)<br />

Else<br />

sum = sum - price(Index)<br />

End If<br />

lblAmount.Caption = FormatCurrency(sum)<br />

End Sub<br />

The Value property of a check box also can be set to “2-Grayed”. When a grayed square<br />

is clicked, it becomes unchecked. When clicked again, it becomes checked.<br />

THE OPTION BUTTON CONTROL<br />

Option buttons are used to give the user a single choice from several options. Normally, a<br />

group of several option buttons is attached to a frame or picture box with the single-clickdraw<br />

technique. Each button consists of a small circle accompanied by text that is set with the<br />

Caption property. When a circle or its accompanying text is clicked, a solid dot appears in the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!