Array of control

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?
f_asmaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iboutchkineCommented:
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
anymore.

Alternative syntax:

dim MyTextboxes as textbox()
'...later
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
    Next        
End Sub

'Then, create a ButtonHandler to handle the button clicks

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RonaldBiemansCommented:
control arrays aren't supported in vb.net

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
next

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
etc..





0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.