CIS 338: Control Arrays Dr. Ralph D. Westfall June, 2011 What Is a Control Array? it is a group of controls: part of a collection in a .NET form all identified as elements of Me.Controls array each has a different Index e.g., Me.Controls(0) , Me.Controls(1) etc. Dropped From, Added To VB.NET control arrays were in VB 6, but not in earlier versions of .NET however you can set up controls in earlier .NET code that work like VB6 control arrays link link2 VB.NET Control Array Code Dim i As Integer For i = 0 To Me.Controls.Count – 1 MsgBox(Me.Controls(i).ToString) Next Following Slides Need Updating Control Array Disadvantages since all members use the same procedure, may need more complicated logic to handle events Private Sub Command1_Click(Index As _ Integer) Select Case Index Case 0 'user clicked cmdTest(0) button Print "zero" Case 1 'user clicked cmdTest(1) button Print "one" End Select End Sub Creating a Control Array (VB6) create first control, and at the very first change the name change any other properties that you want to be the same on all elements e.g., Text select it, then [Ctrl] C or Edit>Copy [Ctrl] V or Edit>Paste to duplicate click Yes on dialog box move copied control where you want it Programming Control Arrays Dim nI as Integer Private Sub Text1_Change(Index As Integer) For nI = Text1.LBound To Text1.UBound If nI = Index Then Text1(nI).BackColor = "&H000000FF" End If Next nI End Sub • LBound is lowest Index in array (usually 0) • UBound is highest Index in array Using Parallel Arrays Sub Text1_GotFocus(Index As Integer) Label1(Index).Font.Bold = True End Sub • two arrays: Text1 and Label1 • when Text1(0) gets focus, Label1(0) (same Index) becomes bold • can use LostFocus event to remove bold Adding Controls with Code nI = Text1.UBound Load Text1(nI + 1) Text1(nI + 1).Visible = True Text1(nI + 1).Top = _ Text1(nI).Top + _ Text1(nI).Height + 120 Text1(nI + 1).Left = _ Text1(nI).Left Deleting Controls with Code Private Sub Command2_Click() Dim nI As Integer nI = Text1.UBound Unload Text1(nI) End Sub at run time, can only delete items created by code (previous slide) can delete any items at design time Advantages of a Control Array more efficient – uses less memory all members work with one procedure to add controls to program when it is running, must use control arrays easier to change properties via code can use loops gets around the limit of 254 control names per form (but who needs that many??)
© Copyright 2026 Paperzz