Click here to Skip to main content
Licence 
First Posted 10 Mar 2004
Views 139,049
Bookmarked 36 times

Control Arrays in VB.NET

By ManojRajan | 10 Mar 2004
This article explains control arrays with example.
13 votes, 43.3%
1
1 vote, 3.3%
2
2 votes, 6.7%
3
7 votes, 23.3%
4
7 votes, 23.3%
5
2.88/5 - 30 votes
μ 2.52, σa 3.03 [?]

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

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,

'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

About the Author

ManojRajan

Web Developer

United States United States

Member
I'm ManojRajan, Working as a consultant architect in Tennessee. I have more than 8 years of experience in Microsoft Technologies.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWorking Example Pinmemberdeliot7:57 17 Nov '10  
GeneralControl Arrays in VB.NET PinmemberBill Ruf11:41 22 Jun '09  
GeneralControl array in VB.NET Pinmembermanish_ahuja11:54 6 Apr '09  
GeneralHi ManojRajan Pinmemberhugosanguino10:53 8 Aug '08  
GeneralControl Arrays in VB.NET by Relman PinmemberRelman5:13 10 Nov '04  
GeneralArticle is not complete PinmemberFade (Amit BS)8:40 24 Mar '04  
GeneralRe: Article is not complete PinmemberManojRajan18:37 24 Mar '04  
GeneralRe: Article is not complete PinmemberFade (Amit BS)6:43 14 Apr '04  
GeneralRe: Article is not complete PinmemberManojRajan19:51 14 Apr '04  
GeneralRe: Article is not complete PinmemberFade (Amit BS)15:56 17 Apr '04  
GeneralRe: Article is not complete PinmemberDuane Flanders8:48 19 Aug '04  
GeneralRe: Article is not complete PinmemberManojRajan20:00 19 Aug '04  
RantRe: Article is not complete PinmemberNeville Nazerane21:08 25 Jul '10  
GeneralRe: Article is not complete PinsussAnonymous2:01 20 Aug '04  
GeneralRe: Article is not complete Pinmemberjaman4dbz13:06 19 May '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 11 Mar 2004
Article Copyright 2004 by ManojRajan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid