Accessing Controls by Their Names or Indices
The preferred way to access controls is via the auto-generated member variables such as Me.TextBox1
in a form. The function FindControl
takes a form or container control and a string containing the name of the control to be obtained and returns a reference to the control:
Usage:
That the procedure listed above is rather slow. To access a lot of controls by name very often references to the controls can be stored in a Hashtable
object. The name of the control is used as the key:
Adding a control:
Looking for a control:
Removing a control:
Sometimes it is even better to add the controls to an array. This will allow fast and easy index-based access to the controls:
Access by MyLabels(0)
to MyLabels(9)
.
Control arrays as known from Visual Basic 6.0 are not available in Visual Basic .NET 2003 and Visual Basic 2005.