Control Arrays are Collections

Control Arrays are Collections

Most VB4 programmers know they can access a form’s control collection using a For Each loop. Many, however, are not aware that individual control arrays are actually collections. This knowledge can be helpful when checking the properties of several controls, especially when new controls are loaded at run time. You might wish to create a form in which all text boxes must be filled out. Instead of assigning a unique name to each text box, create a control array with each text box named txtFields. You can use this name as you use the name of any collection in a For Each loop:

 Dim v	For Each v In txtFields		If v.Text = "" Then			MsgBox "All fields must be " & _				"filled out.", vbExclamation + _				vbOKOnly			Exit Sub		End If	Next

Because txtFields is a collection, you can also determine how many text boxes are in the array by checking the value of txtFields.Count. For more information, see Karl E. Peterson’s Q&A column, “The Keys to Your Controls” [VBPJ August 1997].


Share the Post: