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.

sales = 0<br />

For depNum = 0 To 4<br />

sales = sales + Val(txtSales(depNum).Text)<br />

Next depNum<br />

picTotal.Cls<br />

picTotal.Print “Total sales were ”; FormatCurrency(sales)<br />

End Sub<br />

[Run, type the following data into the text boxes, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

■ CONTROL ARRAY EVENT PROCEDURES<br />

In Section 3 we discussed several events related to text boxes. One example was the GotFocus<br />

event procedure. If txtBox is an ordinary text box, then the GotFocus event procedure begins<br />

with the statement<br />

Private Sub txtBox_GotFocus()<br />

If, on the other h<strong>and</strong>, we make txtBox a control array, the GotFocus event procedure begins with<br />

the statement<br />

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

Two points should be noted. First, even though we may have a dozen or more elements in the<br />

txtBox( ) control array, we will have just one txtBox_GotFocus event procedure to deal with.<br />

Second, <strong>Visual</strong> <strong>Basic</strong> passes to this one event procedure the value of the Index property for the<br />

element of the control array that has just received the focus. We may wish to respond in the<br />

same manner whenever any element of txtBox( ) has the focus, in which case we simply ignore<br />

the value of Index. If, on the other h<strong>and</strong>, we wish to respond in different ways, depending on<br />

which element has the focus, then we write the GotFocus event procedure in the form<br />

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

Select Case Index<br />

Case 0<br />

action when txtBox( ) gets the focus<br />

Case 1<br />

action when txtBox(1) gets the focus . .<br />

End Select<br />

End Sub<br />

In general, all event procedures for a control array have the additional parameter Index As<br />

Integer. This additional parameter can be used, if desired, to base the action taken by the event<br />

procedure on which element of the control array underwent the event.<br />

EXAMPLE 2<br />

The following program creates an electronic dialing pad. The form contains a control array of 10 comm<strong>and</strong><br />

buttons. Each time a comm<strong>and</strong> button is clicked, the Index parameter conveys the digit to be added<br />

onto the phone number. This program illustrates using the Index parameter without employing a Select<br />

Case statement.<br />

Control Arrays 181

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

Saved successfully!

Ooh no, something went wrong!