VB.NET Control Array- Declaration and Application

An array is a collection of elements of same data type. VB.NET Control Array is an array whole elements are VB.NET controls of same type. In this post you will understand how to create VB.NET Control Array and apply in a .NET application.

What is a VB.NET Control Array?

A control arrays in VB.NET is an array of controls. These controls can be buttons, labels, text boxes,  radio-buttons, check-boxes, or any other control that you want to use in your form application. A control array can be seen as a group of VB.NET controls of one type sharing the name and event procedures.

Each element of a control array or each control added to a control array is identified by an index or subscript given after the name of the array with in parentheses

Creating VB.NET Control Array

The Control Array can be created only in code not in design. They are created in the same way as you create an array using the built in data types. The syntax of declaring a control  is as follows

Dim arrayName(integer upperBound) As ControlType

                       or

  Dim arrayName() As ControlType

arrayName is the name of the array

upperBound- the upperbound value in integer. The number of elements is one more than upperbound value

ControlType– is the VB.NET control whose array you need to create. It can be any control type available in VB.NET tools box.

These are control array declaration statements

                Dim lblMessage(10) As Label                      ‘ a control array of 11 labels

                Dim txtValues(8) As TextBox                      ‘ a control array of 9 Textboxes

                Dim btnAction() As Button                           ‘ a control array of Buttons.

                Dim rdbSelect(4) As RadioButton              ‘ a control array of  5 Radiobuttons.

Advantages of Control Arrays

  • Control arrays provide convenience to handle control elements of same type and function.
  • For all the controls in the control array, a single event handler needs to be written. The index argument passed in the event procedure is used to identify the control that fired the event to be executed. It helps in reducing the code size considerably.
  • The most important advantage is that control elements can be added dynamically at the time of execution which were not added at design time.
  • Elements of control array share all the properties and event procedures. So, they require lesser resources than individual control elements. This is another reason of code size reduction

Example

 In this example, you will create a form that allows you to enter total sales for five years in textboxes. On clicking Calculate button, all the sales values of 5 years are added and displayed in the label under the button.

The labels and textboxes for reading sales values are created as Control Arrays of labels and textboxes respectively.

Design of Data Entry Form

In the design of the form, calculate Button(btnCalculate) and Label (lblTotal) for displaying the total sales of 5 years.

Design form

Data Entry Form at Runtime

When the form is executed, control arrays for Labels and Textboxes are declared. In the form_load event these control arrays elements are added and displayed on the form. When a user fills the values in textboxes and clicks the Calculate Button   a for loop is used to get values from textbox control array and add them together in a variable total.  After completing loop label  lblTotal’s  text property is set with variable total’s value.

Runtime VB.NET control array
Public Class Form1
    Dim lblValues(4) As Label 'control array of labels
    Dim txtValues(4) As TextBox 'control array of textboxes
    Dim intCount As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For intCount = 0 To 4
            ' Delcare text box and label variable that are added as elements of control arrays
            Dim MyTextbox As New TextBox
            Dim MyLabel As New Label
            'Set left and top values of labels to define display position in the form
            MyLabel.Left = 10
            MyLabel.Top = 10 + 25 * intCount
            'set the text of labels
            MyLabel.Text = "Year " + CStr(intCount + 1)
            'set width of the labels
            MyLabel.Width = 50
            'add newly created label as an element at the current index defined by intCount of the label control array
            lblValues(intCount) = MyLabel
            'add the label control array element on the form using controls collection
            Me.Controls.Add(lblValues(intCount))
            'Set left and top values of textboxes to define display position in the form
            MyTextbox.Left = 100
            MyTextbox.Top = 10 + 25 * intCount
            'add newly created textbox as an element at the current index defined by intCount of the textbox control array
            txtValues(intCount) = MyTextbox
            'add the textbox control array element on the form using controls collection
            Me.Controls.Add(txtValues(intCount))
        Next
    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim total As Double ' variable to store sum of sales while iterating through control array of textboxes
        For intCount = 0 To 4
            'Add values from textboxes to variable total using += assignment operator
            total += Val(txtValues(intCount).Text)
        Next
        'change fore color of display label
        lblTotal.ForeColor = Color.Red
        'display total in label at form bottom
        lblTotal.Text = "Total 5 years sales is " + CStr(total)
    End Sub
End Class

Be First to Comment

Leave a Reply

Your email address will not be published.