I've created a user control with a simple text box and a corresponding label and added a few custom control properties. When added to a form, I'm trying to loop through all control of this type and evaluate the custom property. In the below, the cntl.MyCustomProperty is not recognized.

I can evaluate the property when I check each user control on the form by name, but not by using the loop below. I would much rather use the loop as it is much cleaner.

Any ideas?

For Each cntl As Control In Me.Controls
            If (TypeOf cntl Is MyUserControl) And cntl.MyCustomProperty = "ABC" Then
                ''Do some stuff
            End If
        Next

try this:

For Each cntl As Control In Me.Controls
      If (TypeOf cntl Is MyUserControl) AndAlso CType(cntl,MyUserControl).MyCustomProperty = "ABC" Then
             ''Do some stuff
      End If
Next

Thanks Geek. Worked like a champ.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.