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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

ready to reproduce as many copies as you desire. To create a copy, press Ctrl+V (or open the<br />

Edit menu <strong>and</strong> select Paste). The copy of txtBox(0) appears in the upper-left corner of the<br />

form. The value of the Index property for this new text box is 1; thus the text box is referred<br />

to as txtBox(1). Move this text box to the desired position. Press Ctrl+V again <strong>and</strong> another<br />

copy of txtBox(0) appears in the upper left corner of the form. Its Index property is 2. Move<br />

txtBox(2) to an appropriate position. Continue copying txtBox(0) in this manner until all<br />

desired controls have been created.<br />

It is important to note that all property settings of txtBox(0) are being passed (as default<br />

settings) to the other elements of the txtBox( ) control array, with the exception of the Index,<br />

Top, <strong>and</strong> Left settings. Thus, as a matter of efficiency, before you begin copying txtBox(0),<br />

set all properties that you want carried over to all elements of txtBox( ). For example, if you<br />

desire to have the Text property blank for all txtBox( ) elements, set the Text property of<br />

txtBox(0) to (blank) before starting the copying process.<br />

The preceding discussion gave a process for creating an array of text boxes. This same<br />

process applies to creating arrays of labels or any other control. In summary, the following<br />

steps create an array of controls while designing a form:<br />

1. Add one instance of the desired control to the form.<br />

2. Set the Index property of this control to 0.<br />

3. Set any other properties of the control that will be common to all elements of<br />

the array.<br />

4. Click on the control <strong>and</strong> then press Ctrl+C to prepare to make a copy of the control.<br />

5. Press Ctrl+V to create a copy of the control. Position this control as desired.<br />

6. Repeat Step 5 until all desired elements of the control array have been created.<br />

EXAMPLE 1<br />

A department store has five departments. The following program requests the amount of sales for each<br />

department <strong>and</strong> displays the total sales. We use a control array of five labels <strong>and</strong> a control array of five<br />

text boxes to h<strong>and</strong>le the input. For the label captions we use “Department 1”, “Department 2”, <strong>and</strong> so on.<br />

Because these labels are the same except for the number, we wait until run time <strong>and</strong> use a For...Next loop<br />

inside the Form_Load ( ) event procedure to assign the captions to each element of the lblDepart( ) control<br />

array. At design time, before making copies of lblDepart(0), we set the Alignment property to “1 –<br />

Right Justify” so that all elements of the array inherit this property. Similarly, the Text property of<br />

txtSales(0) is set to (blank) before copying.<br />

Object Property Setting<br />

frm7_3_1 Caption (blank)<br />

lblDepart() Index 0 to 4<br />

Alignment 1 – Right Justify<br />

txtSales( ) Index 0 to 4<br />

Text (blank)<br />

cmdCompute Caption Compute Total Sales<br />

picTotal<br />

Private Sub Form_Load()<br />

Dim depNum As Integer<br />

For depNum = 0 To 4<br />

lblDepart(depNum).Caption = “Department” & Str(depNum + 1)<br />

Next depNum<br />

End Sub<br />

Private Sub cmdCompute_Click()<br />

Dim depNum As Integer, sales As Single

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

Saved successfully!

Ooh no, something went wrong!