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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: