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.

If controlName is the name of a control whose Index property was assigned a value during<br />

form design (thus creating the beginnings of a control array) <strong>and</strong> num is a whole number<br />

that has not yet been used as an index for the controlName( ) array, then the statement<br />

Load controlName(num)<br />

copies all the properties of controlName(0), including the Top <strong>and</strong> Left properties, <strong>and</strong> creates<br />

the element controlName(num) of the controlName( ) array. The only property of<br />

controlName(num) that may differ from that of controlName(0) is the Visible property. The<br />

Load statement always sets the Visible property of the created element to False. After creating<br />

a new element of a control array, you will want to adjust the Top <strong>and</strong> Left properties so that<br />

the new element has its own unique location on the form, <strong>and</strong> then set the Visible property of<br />

the new element to True.<br />

EXAMPLE 3<br />

Write a program to create a control array of 12 labels <strong>and</strong> a control array of 12 text boxes. Position the<br />

labels <strong>and</strong> text boxes so that they form two columns, with the labels to the left of the text boxes <strong>and</strong> the<br />

text boxes one immediately below the other. Use text boxes whose height is as small as possible. Use<br />

labels whose height is just large enough to display a single line. Assign the captions Jan, Feb, <strong>and</strong> so on,<br />

to the labels.<br />

SOLUTION:<br />

When designing the form, we place the first label to the left of the first text box <strong>and</strong> set the Index property<br />

of both controls to 0. The height of the shortest text box is 288 units <strong>and</strong> the height of a label just tall<br />

enough for a single line is 252 units. We use the text box’s Height property as the unit of vertical spacing<br />

for both the new text box elements <strong>and</strong> the new label elements. The following is the form at design time<br />

<strong>and</strong> at run time.<br />

Object Property Setting<br />

frm 7_3_3 Caption Year<br />

lblMonth() Index 0<br />

Caption Jan<br />

Height 252<br />

txtInfo() Index 0<br />

Text (blank)<br />

Height 288<br />

Private Sub Form_Load()<br />

Dim i As Integer, monthNames As String<br />

monthNames = “FebMarAprMayJunJulAugSepOctNovDec”<br />

For i = 1 To 11<br />

Load lblMonth(i)<br />

Load txtInfo(i)<br />

lblMonth(i).Top = lblMonth(i - 1).Top +<br />

txtInfo(0).Height txtInfo(i).Top = txtInfo(i - 1).Top + txtinfo(0).Height<br />

lblMonth(i).Caption = Mid(monthNames, 3 * i - 2, 3)<br />

lblMonth(i).Visible = True<br />

txtInfo(i).Visible = True<br />

Next i<br />

End Sub<br />

Control Arrays 183

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

Saved successfully!

Ooh no, something went wrong!