Array of control

Posted on 2004-11-30
Last Modified: 2010-04-23
I used to use array of controls in VB6 such as image control to share the same code between them and use index of the array to differentiate between them. Can I do the same in VB.NET?
Question by:f_asmaa
    LVL 28

    Accepted Solution

    VB.NET  no longer supports control arrays(control collection that share the same name).
    However you can continue to store controls in array if the array is declared in the object type

    To process several controls (for example 20 buttons)

    Private Sub MyButtonHandler_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click,
    Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click,
    Button9.Click, Button10.Click, Button11.Click, Button12.Click,
    Button13.Click, Button14.Click, Button15.Click, Button16.Click,
    Button17.Click, Button18.Click, Button19.Click, Button20.Click

    also you can do

    dim MyTextboxes as textbox() = {txt1, txt2, txt3}

    Now they are in an array. Of course, the code has to be executed after the
    call to InitializeComponent. Control arrays like in VB6 are not required

    Alternative syntax:

    dim MyTextboxes as textbox()
    MyTextboxes = New textbox(){txt1, txt2, txt3}

    Here is an example how to create a control array and handler to it

    Private ButtonArray(9) As Button

    Public Sub AddButtons()
        For x As Integer = 0 To 9
            ButtonArray(x) = New Button
            With ButtonArray(x)
                .Tag = x
                .Text = "Button " & x
                .Size = New Size(60,20)
                .Location = New Point(20, (x * 20) + 4)
                .Parent = Me
                .Visible = True
                AddHandler .Click, AddressOf ButtonHandler
            End With
    End Sub

    'Then, create a ButtonHandler to handle the button clicks

    Public Sub ButtonHandler(ByVal sender As System.Object, ByVal e As
        MsgBox("Button number " & CType(sender,Button).Tag & " was clicked")
    End Sub
    LVL 25

    Expert Comment

    control arrays aren't supported in

    but you can get to them like

    for each x as control in me.controls
                  If x.GetType Is GetType(TextBox) Then
                    CType(x, TextBox).Text = "test"
                End If

    And if you want to use the same event for several controls

    you can do something like this

        Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.TextChanged, textbox2.TextChanged
                 ctype(sender,textbox).text += "ok"
        End Sub

    or you can create your own array

    dim mytextboxes(10) as textbox

    mytextboxes(0) = textbox1
    mytextboxes(1) = textbox2


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now