Introduction
This is a demonstration of a method to create an array of like-named controls that are created and placed on a form at design-time, not dynamically added at run-time.
I developed this when converting a VB6 app that used control arrays extensively to VB.NET, which as we all know, does not support control arrays in the same way VB6 did.
Normally, I would create the controls in Form_Load
code, setting size and location, and adding to an array.
However, the project I was working on had several arrays of controls that were not evenly spaced on the form, so I could not use nice little loops incrementing x
and y
locations, etc. Additionally, I was having to make frequent changes regarding the number and placement of controls.
So I decided to go at it from a different direction. Now, I add all controls at design-time, placing and sizing them as needed. But, I name them all with a common base name plus a delimiter and a numeric extension.
Examples: btnGroupA_00
, btnGroupA_01
, btnGroupA_02
Then in Form_Load
, I process them all into an array, assigning the numeric portion of the control name to the Tag
property and assigning a shared Click
Event.
Now, I have an array named btnGroupA()
of buttons, all sharing one Click
procedure.
This code uses generic Control
object types because all I needed were Click
events.
To try the attached code, start a new VB project and add some buttons to Form1
named btnTest_00
, btnTest_01
, btnTest_02
, etc. and some CheckBox
es named chkTest_00
, chkTest_01
, ...
Background
A different approach to creating control arrays in VB.NET.
Using the Code
To try the attached code, start a new VB project and add some buttons to Form1
named btnTest_00
, btnTest_01
, btnTest_02
, etc. and some CheckBox
es named chkTest_00
, chkTest_01
.
Public Class Form1
Dim myButtons(0) As Control
Dim myCheckBoxes(0) As Control
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myButtonClickProc As New System.EventHandler(AddressOf myButtons_Click)
Dim myCheckboxClickProc As New System.EventHandler(AddressOf myCheckbox_Click)
MakeControlArray(Me, "btnTest", myButtons, myButtonClickProc)
MakeControlArray(Me, "chkTest", myCheckBoxes, myCheckboxClickProc)
myButtons(1).BackColor = Color.Red
End Sub
Private Sub MakeControlArray(ctlContainer As Control, _
ctlName As String, _
ByRef ctlArray() As Control, _
clickDelegate As System.EventHandler)
Dim ctrlDict As Dictionary(Of Integer, Object) = New Dictionary(Of Integer, Object)
For Each ctl As Control In ctlContainer.Controls
If ctl.Name.Contains("_") Then
Try
Dim parts() As String = ctl.Name.Split("_")
If parts.Count = 2 Then
If parts(0) = ctlName Then
ctrlDict.Add(Val(parts(1)), ctl)
End If
End If
Catch
End Try
End If
Next
If ctrlDict.Count > 0 Then
ReDim ctlArray(ctrlDict.Count - 1)
Dim keys As List(Of Integer) = ctrlDict.Keys.ToList
keys.Sort()
Dim pos As Integer = 0 Dim x As Integer
For Each x In keys
Dim b As New Control
If (ctrlDict.TryGetValue(x, b)) Then
b.Tag = pos AddHandler b.Click, clickDelegate ctlArray(pos) = b
pos += 1
End If
Next
End If
End Sub
Public Sub myButtons_Click(sender As Object, e As EventArgs)
MessageBox.Show("Button: " + sender.tag.ToString)
End Sub
Public Sub myCheckbox_Click(sender As Object, e As EventArgs)
MessageBox.Show("Checkbox: " + sender.tag.ToString)
End Sub
End Class
Points of Interest
A different approach to creating control arrays in VB.NET.
History
- 4th September, 2015: Initial version