Using Control Collections

Using Control Collections

I created a collection of textbox controls within the same form, using the usual copy option from the edit menu. This collection seems Ok.I try to set properties for these textboxes with a For Each loop in the Form_Load() event for the same form.This gives an error message that my name for the textboxes is not defined.I tried to fix this with a Dim BoxName As Control in the general section for the form and got an error message that the item is already defined.

Instead of declaring your loop variable as a Control, declare it as anobject. That’s the recommended way to traverse the Controls collection.Your code will look something like this:

Dim loopCtl as ObjectFor Each loopCtl in myForm.Controls   If TypeOf(loopCtl) Is TextBox Then      loopCtl.Enabled = True      … other property settings …   End IfNext loopCtl
This lets you look at all the controls on the form to see if they areTextBox controls before you attempt to set any properties on them.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist