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
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
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