Avatar of michael_krell
michael_krell
 asked on

Visual Basic 2005. processing Objects in a Control Collection

I am trying to make a sub function that hides all of a specified object on a form. I am trying to hide labels currently. I wrote the code fairly simply, but i cant seem to find a property inside of the Control object where a certain object can be distinguished.

Can someone tell me what property in the Control data type can be used to distinguish what the object is? "for example: text box, label, etc....."

Thanks
Public Sub HideShowlabels(ByVal Visible As Boolean)
        Dim CtrlVar As Control
        If Visible Then
            For Each CtrlVar In Controls
                CtrlVar.Visible = True
            Next
        Else
            For Each CtrlVar In Controls
                CtrlVar.Visible = False
            Next
        End If
    End Sub

Open in new window

.NET ProgrammingVisual Basic ClassicRouters

Avatar of undefined
Last Comment
Mike Tomlinson

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
appari

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
michael_krell

ASKER
Awesome! I had to make some changes to what you wrote because the comparison needed to be inside the "For Each"
    Public Sub HideShowlabels(ByVal Visible As Boolean)
        Dim CtrlVar As Control
 
        If Visible Then
            For Each CtrlVar In Controls
                If TypeOf CtrlVar Is Label Then CtrlVar.Visible = True
            Next
        Else
            For Each CtrlVar In Controls
                If TypeOf CtrlVar Is Label Then CtrlVar.Visible = False
            Next
 
        End If
    End Sub

Open in new window

michael_krell

ASKER
Had to change the code slightly to get it to work.
Mike Tomlinson

You could simplify that to:
    Public Sub HideShowlabels(ByVal VisibleState As Boolean)
        For Each CtrlVar As Control In Me.Controls
            If TypeOf CtrlVar Is Label Then
                CtrlVar.Visible = VisibleState
            End If
        Next
    End Sub

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes