Using Control Arrays

Control Arrays allow you to have a group of controls that can be accessed by the same name. Often, you might find that you need to specify a control to access at runtime... instead of having to create seperate code to do cmdAction1.Enabled... and cmdAction2.Enabled etc, you can use a Control array. Then, you could simply call cmdAction(i).Enabled where i is the number of the control in the array.

To create a control array, simply set the name of one control, and set its Index property to 0. Then, change the name of another control to the same name - this will then automatically be given an Index of 1. You can now access the control by calling objControlName(Index).

When controls are in an array, they also share the same event procedures... Instead of having cmdAction1_Click, cmdAction2_Click and so on, you can have cmdAction_Click(Index As Integer). This event is then triggered by VB each time on of the controls in the array is clicked. You can find out which control it is by checking the Index parameter.

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth