Enable And Disable All Controls In A Frame

This example will show you how to enable and disable all controls in a frame. It's useful if you need to disable and enable several controls together. Just insert them to the same frame, and use the code below.


Insert 1 Frame (named Frame1) and 2 Command Buttons (named Command1 and Command2) to your form.
Insert some controls to the frame (buttons, text boxes, whatever you want).
When you will press the first button, All the controls in the frame will be disabled, And
when you will press the second button, all the controls will be enabled.

Form Code

Public Sub EnableFrame(InFrame As Frame, ByVal Flag As Boolean)
    Dim Contrl As Control
'some controls don't have the Container.Name property, so instead of
'stopping the application with an error message, we ignore them.
On Error Resume Next
'enable or disable the frame that passed as parameter. 
InFrame.Enabled = Flag
'passing over all controls
    For Each Contrl In InFrame.Parent.Controls
'if the control is found in the frame
       If (Contrl.Container.Name = InFrame.Name) Then
'if the control is a frame, and it's not the frame that passed as parameter, i.e.
'other frame that found inside our frame, recursively run this sub with this frame,
'to enable or disable all the controls in it.

          If (TypeOf Contrl Is Frame) And Not (Contrl.Name = InFrame.Name) Then
             EnableFrame Contrl, Flag
'enable or disable the control
             If Not (TypeOf Contrl Is Menu) Then Contrl.Enabled = Flag
          End If
       End If
End Sub

Private Sub Command1_Click()
    EnableFrame Frame1, False
End Sub

Private Sub Command2_Click()
    EnableFrame Frame1, True
End Sub

Go Back