How To Get Control Property by "String Name"?

As a quick useful tip to note, you don't seem to have to specify the type of control within the CType statement for purposes of accessing a control on your form. I came across this when trying to access multiple types of form controls, such as buttons and textboxes, all with the same line of code.

CType(Controls("NAME_OF_CONTROL"), Control)

Note that, rather than specifying exactly what type of control, such as 'TextBox' or 'Button', you simply state 'Control'. This allows you to universally change any type of control, without needing to specify its type.

I couldn't find this anywhere else, so I thought I'd share it!


You can access the controls by name via the Form.Controls property, for instance:

Dim text1 As TextBox = CType(Me.Controls("text_1"), TextBox)