Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of michael_krell
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

Had to change the code slightly to get it to work.
Avatar of 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