Control Array is One of the VB6 features that are no longer supported in VB.NET like automated creation of control arrays by simply copying an existing control as many times as required into an array. There is existing article in MSDN which illustrates how to create control arrays at run time, but this may not be a suitable approach in all cases.
I needed control array specially in textbox control because i'm doing data entry program with a lot of textbox, we want to simplify the coding structure of event handler like keyup, keydown, keypress, gotfocus, validation etc. With the help of open forum i found source code that easy to use and i will also share to others. Thanks to the original author of this Class ControlArray, it helps a lot for me. (see sample code below).
******* FORM ********
Option Explicit On
Imports System
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Private aTextBoxes() As TextBox
Private sFldName as String
Private sInputBox as String
Private iFldIndex as Integer
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'create 6 textboxes in the Form1 name properties as:
'tbInput1
'tbInput2
'tbInput3
'tbInput4
'tbInput5
'tbInput6
Initialized_ArrayTextBox()
End Sub
Private Sub Initialized_ArrayTextBox()
Dim i As Integer = 1
aTextBoxes = ControlArray.getControlArray(Me, "tbInput")
For i = 1 To 6
AddHandler aTextBoxes(i).KeyPress, AddressOf tbInput_KeyPress
AddHandler aTextBoxes(i).KeyDown, AddressOf tbInput_KeyDown
AddHandler aTextBoxes(i).KeyUp, AddressOf tbInput_KeyUp
AddHandler aTextBoxes(i).GotFocus, AddressOf tbInput_GotFocus
AddHandler aTextBoxes(i).LostFocus, AddressOf tbInput_LostFocus
AddHandler aTextBoxes(i).TextChanged, AddressOf tbInput_TextChanged
AddHandler aTextBoxes(i).Validating, AddressOf tbInput_Validating
Next i
End Sub
Private Sub tbInput_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbInput1.KeyPress
'code here
End Sub
Private Sub tbInput_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbInput1.KeyDown
'code here
End Sub
Private Sub tbInput_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles tbInput1.GotFocus
'textbox properties tag
sFldName = sender.tag
'textbox properties name
sInputBox = sender.name
'Identify the current textbox index
iFldIndex = Microsoft.VisualBasic.Right(sInputBox, Len(sInputBox) - 7)
aTextBoxes(iFldIndex).SelectionStart = 0
aTextBoxes(iFldIndex).SelectionLength = Len(aTextBoxes(iFldIndex).Text)
End Sub
Private Sub tbInput_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbInput1.KeyUp
'code here
End Sub
Private Sub tbInput_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles tbInput1.LostFocus
'code her
End Sub
Private Sub tbInput_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles tbInput1.TextChanged
'code here
End Sub
Private Sub tbInput_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles tbInput1.Validating
'code here
End Sub
End Class
*********Class Module*********
Public Class ControlArray
Public Shared Function getControlArray(ByVal frm As Windows.Forms.Form, _
ByVal controlName As String) As System.Array
Dim i As Short
Dim startOfIndex As Short
Dim alist As New ArrayList
Dim controlType As System.Type
Dim ctl As System.Windows.Forms.Control
Dim strSuffix As String
Dim maxIndex As Short = -1 'Default
'Loop through all controls, looking for controls with the matching name pattern
'Find the highest indexed control
Dim allControls As ArrayList = GatherAllControls(frm)
For Each ctl In allControls
startOfIndex = ctl.Name.ToLower.IndexOf(controlName.ToLower)
If startOfIndex = 0 Then
strSuffix = ctl.Name.Substring(controlName.Length)
If IsInteger(strSuffix) Then 'Check that the suffix is an integer (index of the array)
If Val(strSuffix) > maxIndex Then maxIndex = Val(strSuffix) 'Find the highest indexed Element
End If
End If
Next ctl
'Add to the list of controls in correct order
If maxIndex > -1 Then
For i = 0 To maxIndex
Dim aControl As Control = getControlFromName(allControls, controlName, i)
If Not (aControl Is Nothing) Then
controlType = aControl.GetType 'Save the object Type (uses the last control found as the Type)
End If
alist.Add(aControl)
Next
End If
Return alist.ToArray(controlType)
End Function
Public Shared Function GatherAllControls(ByVal cntrl As Control) As ArrayList
Dim list As New ArrayList
If Not (TypeOf cntrl Is Form) Then
list.Add(cntrl) 'Add the control itself (Could be a container control or the form itself!)
End If
If cntrl.HasChildren Then
For Each cControl As Control In cntrl.Controls
Dim subList As ArrayList = GatherAllControls(cControl)
list.AddRange(subList)
Next
End If
Return list
End Function
'Converts any type of like named controls on a form to a control array
'by using the notation ControlName_1, ControlName_2, where the
'can be replaced by any separator string
Public Shared Function getMixedControlArray(ByVal frm As Windows.Forms.Form, _
ByVal controlName As String, Optional ByVal separator As String = "") As Control()
Dim i As Short
Dim startOfIndex As Short
Dim alist As New ArrayList
Dim ctl As System.Windows.Forms.Control
Dim strSuffix As String
Dim maxIndex As Short = -1 'Default
'Loop through all controls, looking for controls with the matching name pattern
'Find the highest indexed control
Dim allControls As ArrayList = GatherAllControls(frm)
For Each ctl In allControls
startOfIndex = ctl.Name.ToLower.IndexOf(controlName.ToLower & separator)
If startOfIndex = 0 Then
strSuffix = ctl.Name.Substring(controlName.Length & separator)
If IsInteger(strSuffix) Then 'Check that the suffix is an integer (index of the array)
If Val(strSuffix) > maxIndex Then maxIndex = Val(strSuffix)
'Find the highest indexed Element
End If
End If
Next ctl
'Add to the list of controls in correct order
If maxIndex > -1 Then
For i = 0 To maxIndex
Dim aControl As Control = getControlFromName(allControls, controlName, i)
alist.Add(aControl)
Next
End If
Return alist.ToArray(GetType(Control))
End Function
Private Shared Function getControlFromName(ByRef frm As Windows.Forms.Form, _
ByVal controlName As String, ByVal index As Short) As System.Windows.Forms.Control
controlName = controlName & index
For Each ctl As Control In frm.Controls
If String.Compare(ctl.Name, controlName, True) = 0 Then
Return ctl
End If
Next ctl
Return Nothing
End Function
Private Shared Function getControlFromName(ByVal allControls As ArrayList, _
ByVal controlName As String, ByVal index As Short) As System.Windows.Forms.Control
controlName = controlName & index
For Each ctl As Control In allControls
If String.Compare(ctl.Name, controlName, True) = 0 Then
Return ctl
End If
Next ctl
Return Nothing
End Function
Private Shared Function IsInteger(ByVal Value As String) As Boolean
If Value = "" Then Return False
For Each chr As Char In Value
If Not Char.IsDigit(chr) Then
Return False
End If
Next
Return True
End Function
End Class
FREE PDF BOOK DOWNLOAD