Control Array in VB 2005

Simple steps for generating control Array in VB.NET

Introduction

I will show you a way to generate control arrays.

I created two classes One for generating Control Array and the other one for sorting the arrays according to the tab indexes. Control Arrays are required in many development scenarios. e.g storing control's data in xml file or sending data via the xml string throughout the application. etc etc.

Generating Control Array can save a lot of valuable time of the developers. Here I will try to explain -

-How to generate control array?
-How to sort the array of controls?
-Implementation.


Look below at the screen shots of a win form. From these windows I need the input type controls like textboxes, checkboxes, RadioButton and comboboxes. These windows have a good mixture of both container and input kinds of controls . Though the controls are nested inside one another, I will generate an array for all the input type controls only.

  • Profile Tab - Edit Company Profile window

  • Default Tab - Edit Company Profile window
Here, I have posted two classes and one implementaion(codes under form_load event) code. Under each classes I have detailed the work. I will advice the readers to go through the complete documentation for each classes before copying the respective codes.

** Before using the codes you must set the correct tab orders for the controls. Use Tab Index property from the Property Window.
  • ControlArrayGenerator.vb: This class will generated arraylist for all the controls from a form.

'@Author Prithiraj Sengupta
Imports System.Collections
Public Class ControlArrayGenerator
Public Function inputArray(ByVal frm)'frm is Form type. inputArray(Me)
Dim ctl As New Control
Dim alist As New ArrayList
Dim CSort As New ControlArraySort
Dim arr As ArrayList
Dim ar As array

For Each ctl In frm.Controls
alist = inputReader(ctl, alist)
Next ctl

arr = CSort.SortControlTabIndex(alist)
ar = arr.ToArray
Return arr
End Function

Private Function inputReader(ByVal ctl, ByVal alist)

Try
If TypeOf ctl Is TextBox Then
alist.Add(ctl)
End If
If TypeOf ctl Is ComboBox Then
alist.Add(ctl)
End If

If TypeOf ctl Is CheckBox Then
alist.Add(ctl)
End If
If TypeOf ctl Is RadioButton Then
alist.Add(ctl)
End If

If TypeOf ctl Is Panel And ctl.Controls.Count >= 1 Then
panelReader(ctl, alist)
End If
If TypeOf ctl Is GroupBox And ctl.Controls.Count >= 1 Then
groupboxReader(ctl, alist)
End If
If TypeOf ctl Is TabControl And ctl.Controls.Count >= 1 Then
TabControlReader(ctl, alist)

End If

Catch ex As Exception
Dim err As String
err = ex.ToString
err = err
End Try
Return alist
End Function

Private Sub panelReader(ByVal ctl, ByVal alist)
For Each ctl In ctl.controls
inputReader(ctl, alist)
Next ctl
End Sub

Private Sub groupboxReader(ByVal ctl, ByVal alist)
For Each ctl In ctl.controls
inputReader(ctl, alist)
Next
End Sub

Private Sub TabControlReader(ByVal ctl, ByVal alist)
For Each ctl In ctl.Controls
inputReader(ctl, alist)
Next
End Sub

End Class

  • ControlArraySort.vb: This class will sort the controls in the arraylist according the tab index of the respective controls.
'@Author Prithiraj Sengupta
Imports
System
Public
Class ControlArraySort
Public Function SortControlTabIndex(ByVal aList As ArrayList)
Dim arrCount As Integer = aList.Count
Dim i, j, Tb, Tb_next As Integer
Try
For i = 1 To arrCount - 1
For j = 1 To arrCount - i
Tb = aList.Item(j - 1).TabIndex
Tb_next = aList.Item(j).TabIndex
If Tb > Tb_next Then
Dim Obj As Object
Obj = aList.Item(j)
aList.Item(j) = aList.Item(j - 1)
aList.Item(j - 1) = Obj
End If
Next j
Next i
Catch ex As Exception
Dim x As String
x = ex.ToString
x = x
End Try
Return aList
End Function

  • Implementation: The codes below are the implementation of ControlArrayGenerator class from Form_Load event of an application form containing number of controls.

'Need to call from the form_load event.
Dim arr As Array
Dim cag As New ControlArrayGenerator
arr=cag. inputArray(Me)


The arr variable in the QuickWatch window:




Explanation :


Imports System.Collections

Need to import system.collections to get the ArrayList Class.


Dim ctl As New Control
Dim alist As New ArrayList
Dim CSort As New ControlArraySort
Dim arr As ArrayList
Dim ar As array



ctl is the object of type Control class. visit
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx for details. Control class is the base class for controls.

alist is the instance of ArrayList class. aList will add the controls on it during excecution of the above code.

cSort is the Object instance for the class ControlArraySort. This class is created to sort the arraylist object of the controls according to the tab indexes of these controls.

arr is also a instance of ArrayList. This variable will have the return sorted list from the ControlArraySort Class.

ar is the final Array containing the sorted list of all the controls. This variable containing the sorted control array will be returned by function inputArray(ByVal frm).



For Each ctl In frm.Controls
alist = inputReader(ctl, alist)
Next ctl

A form can have number of controls like, TabControls, Panels, GroupBoxes, TextBoxes, Buttons, RadioButtons, CheckBoxes, ComboBoxes and many others. So, for each controls the a function inputReader() will be called. This function will return the unordered list of control Arrays. The details for inputReader function is detailed below.

Private Function inputReader(ByVal ctl, ByVal alist)
Try
If TypeOf ctl Is TextBox Then
alist.Add(ctl)
End If
If TypeOf ctl Is ComboBox Then
alist.Add(ctl)
End If
If TypeOf ctl Is CheckBox Then
alist.Add(ctl)
End If
If TypeOf ctl Is RadioButton Then
alist.Add(ctl)
End If
If TypeOf ctl Is Panel And ctl.Controls.Count >= 1 Then
panelReader(ctl, alist)
End If
If TypeOf ctl Is GroupBox And ctl.Controls.Count >= 1 Then
groupboxReader(ctl, alist)
End If
If TypeOf ctl Is TabControl And ctl.Controls.Count >= 1 Then
TabControlReader(ctl, alist)
End If
Catch ex As Exception
Dim err As String
err = ex.ToString
err = err
End Try
Return alist
End Function
Private Sub panelReader(ByVal ctl, ByVal alist)
For Each ctl In ctl.controls
inputReader(ctl, alist)
Next ctl
End Sub
Private Sub groupboxReader(ByVal ctl, ByVal alist)
For Each ctl In ctl.controls
inputReader(ctl, alist)
Next
End Sub
Private Sub TabControlReader(ByVal ctl, ByVal alist)
For Each ctl In ctl.Controls
inputReader(ctl, alist)
Next
End Sub


I have detailed the above codes in two cases. These cases will make it easy to understand the function inputReader.

Case.1.
If a control is of type Text box. When the function inputReader is called from inside the for loop, 1st condition will be satisfied and the code alist.Add(ctl) will be executed. The Object of the text box from the Form will get added to the alist of type ArrayList class.

Case.2.
This will explain the recusive part of the program.
If a control is of type GroupBox and if it contains atleast one control. The condition

If TypeOf ctl Is GroupBox And ctl.Controls.Count >= 1 Then

will get satisfied. So the function groupboxReader(ctl,alist) will get executed. The function inputReader is again called to check whether the groupbox has any input type controls like textboxes, checkboxes and comboxes. If it has any input type controls the control will get added to the alist(of type ArrayList). This case 2 is recursive. This can be well understand by debuging a winform having controls inside the groupboxes.


ControlArraySort.

I used boolean sorting algorithm for sorting the array list according to the Tab Indexes.
The following code be executed to call the sortControlTabIndex(alist) from the ControlArraySort class.

arr = CSort.SortControlTabIndex(alist)

Conclusion:

We have successfully generated by the use of the two simple classes. The Classes are

-ControlArrayGenerator.vb
-ControlArraySort.vb

With these two classes we can generate control arrays for our applications. The codes here can be modified and applied to the applications as needed.