Click here to Skip to main content
15,905,508 members
Articles / Programming Languages / Visual Basic
Article

Control Arrays in VB.NET

Rate me:
Please Sign up or sign in to vote.
3.36/5 (33 votes)
10 Mar 20042 min read 283.3K   38   18
This article explains control arrays with example.

Control Arrays

Control Arrays are arrays of controls sharing a common event handler. That is, you need to write code for only one event, which can handle other controls' events.

For example if you consider an application like calculator, where on the click event of the buttons from 0 to 9, you want to append the text to the visible text. So when you write code for all individual buttons, it is a time consuming process. Because we need the same peace of code for all these buttons, we can create one event handler to handle the events raised by all these buttons.

In Visual Basic 6, this was fairly simple. You have to copy and paste the control and confirm ‘Yes’ when asked, whether to create a control array. You can see the first control automatically gets an index of zero and the following controls get the index incremented by one from the last control. And if you double click on the buttons, you can see all these controls have same event handler, however you can notice a new argument, which is passed to the click event, named Index. This index property is the one, which will tell you which button is clicked (If you want to know, which one clicked). To create this while runtime, you can copy one control at design time and use this button to create other buttons. You can load a control and remove a control dynamically in VB6.

In Dot net the creation of control arrays are easier than the previous versions and are not created by copying and pasting, instead simply add the control events to the Handles list. You can give any name to this procedure.

Example

VB.NET
Private Sub ClickButton(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles Button1.Click, _
  Button2.Click, Button3.Enter
        Dim btn As Button
        btn = CType(sender, Button)
        MsgBox(btn.Text)
End Sub 

In the above example ClickButton procedure is handling the click event of Button1 and Button2, whereas Enter event of the Button3. In order to check the control that is pressed, you need to convert the sender to the respective type. The CType function converts it into a button type, so that you can access the attributes of the event raised by the control.

Now let us see how to create a button in run time,

VB.NET
'Create the button
        Dim btn As New Button()
'Specify the 
location and the size
        btn.Location = New System.Drawing.Point(200, 30)
        btn.Size = New System.Drawing.Size(100, 20)
        btn.Text = "New Button"
'Add it to the forms control collection
               Me.Controls.Add(btn)
'Link the event to the event handler
        AddHandler btn.Click, AddressOf Me.ClickButton 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I'm ManojRajan, Working as a consultant architect in Tennessee. I have more than 8 years of experience in Microsoft Technologies.

Comments and Discussions

 
QuestionNeed Help for getting number of index of control arrays Pin
Glenn Maramis26-May-14 2:18
Glenn Maramis26-May-14 2:18 
GeneralArrays of controls in vb.net Pin
spvasekar1-Sep-12 3:33
spvasekar1-Sep-12 3:33 
GeneralWorking Example Pin
deliot17-Nov-10 6:57
deliot17-Nov-10 6:57 
This article helped me a GREAT DEAL in creating a 3D Tic Tac Toe Game.

Here is some "demo" code I used to test these concepts before I implemented them in my own code. Basically, it dynamically loads 5 command buttons, and sets it so all 5 command buttons are "handled" by the same subroutine. At first, I was pretty upset that Control Arrays were removed, but I think using an "Array Of Controls" with CType and AddHandler is better actually:

0 REM Tuesday, November 16, 2010 10:00 PM
1 REM based largely on these articles:
2 REM (1) http://www.codeproject.com/KB/vb/Control_Arrays.aspx
3 REM (2) http://codebetter.com/blogs/peter.van.ooijen/archive/2005/08/03/130245.aspx
4 REM (3) http://www.devasp.net/net/articles/display/433.html
5
6 Public Class Form1
7 REM AllButtons_Click is a sub I created, note that it doesn't have a "handles" statement at the end
8 Private Sub AllButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
9
10 REM a button object that we will "link" to the actual button
11 Dim TempButton As Button
12
13 REM Convert the "sender" object, which is a System.Object to a Button.
14 REM This makes sure that we can set and check Button properties.
15 TempButton = CType(sender, Button)
16 Label1.Text = "You Pushed The Button Named " & TempButton.Name
17
18 REM Make a visible change to the button pushed.
19 TempButton.Text = "Pushed"
20
21 End Sub
22
23 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
24 REM create an array of 5 buttons to test with...
25 Dim MyButton(5) As Button
26 Dim MyPoint As Point
27 Dim I As Integer
28 Dim Xpos As Integer
29 Dim Ypos As Integer
30
31 REM set initial X and Y location for first button
32 Xpos = 23
33 Ypos = 23
34
35 For I = 1 To 5
36 REM create Point object to use with Location
37 MyPoint.X = Xpos
38 MyPoint.Y = Ypos
39 REM create a new button
40 MyButton(I) = New Button
41 REM assign settings to new button
42 MyButton(I).Name = "Button" & I
43 MyButton(I).Text = "Button" & I
44 MyButton(I).Location = MyPoint
45 MyButton(I).Visible = True
46 REM add new button to "Me" which is Form1
47 Me.Controls.Add(MyButton(I))
48 REM tell Visual Basic which SUB will "handle"
49 REM when the button is clicked. In this case, it
50 REM will be handled by the AllButtons_Click sub!!
51 AddHandler MyButton(I).Click, AddressOf AllButtons_Click
52 REM change Xpos for next button, otherwise, all buttons
53 REM will be ON TOP of each other!!
54 Xpos = Xpos + 100
55 Next I
56
57 End Sub
58
59 End Class

GeneralControl Arrays in VB.NET Pin
Bill Ruf22-Jun-09 10:41
Bill Ruf22-Jun-09 10:41 
GeneralControl array in VB.NET Pin
manish_ahuja6-Apr-09 10:54
manish_ahuja6-Apr-09 10:54 
GeneralHi ManojRajan Pin
hugosanguino8-Aug-08 9:53
hugosanguino8-Aug-08 9:53 
GeneralControl Arrays in VB.NET by Relman Pin
Relman10-Nov-04 4:13
Relman10-Nov-04 4:13 
GeneralArticle is not complete Pin
Fade (Amit BS)24-Mar-04 7:40
Fade (Amit BS)24-Mar-04 7:40 
GeneralRe: Article is not complete Pin
ManojRajan24-Mar-04 17:37
ManojRajan24-Mar-04 17:37 
GeneralRe: Article is not complete Pin
Fade (Amit BS)14-Apr-04 5:43
Fade (Amit BS)14-Apr-04 5:43 
GeneralRe: Article is not complete Pin
ManojRajan14-Apr-04 18:51
ManojRajan14-Apr-04 18:51 
GeneralRe: Article is not complete Pin
Fade (Amit BS)17-Apr-04 14:56
Fade (Amit BS)17-Apr-04 14:56 
GeneralRe: Article is not complete Pin
Duane Flanders19-Aug-04 7:48
Duane Flanders19-Aug-04 7:48 
GeneralRe: Article is not complete Pin
ManojRajan19-Aug-04 19:00
ManojRajan19-Aug-04 19:00 
RantRe: Article is not complete Pin
Neville Nazerane25-Jul-10 20:08
Neville Nazerane25-Jul-10 20:08 
GeneralRe: Article is not complete Pin
Anonymous20-Aug-04 1:01
Anonymous20-Aug-04 1:01 
GeneralRe: Article is not complete Pin
jaman4dbz19-May-06 12:06
jaman4dbz19-May-06 12:06 
GeneralRe: Article is not complete Pin
Craig P Williams Sr16-Mar-23 4:25
professionalCraig P Williams Sr16-Mar-23 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.