Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Apr 22, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 an object. That's the recommended way to traverse the Controls collection. Your code will look something like this:

Dim loopCtl as Object

For Each loopCtl in myForm.Controls
   If TypeOf(loopCtl) Is TextBox Then
      loopCtl.Enabled = True
      ... other property settings ...
   End If
Next loopCtl
This lets you look at all the controls on the form to see if they are TextBox controls before you attempt to set any properties on them.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date