Release focus from all controls

  • Thread starter Thread starter Mike Kampff
  • Start date Start date
M

Mike Kampff

What code would I use to release focus from all controls
on my form? I need to release all focus because I want to
hide all of the controls, leaving only Labels visible. I
don't think it's possible to set the focus to labels.

Is there a way to set the focus to the form itself? Just
like when the form initially opens and if there are no
focus-enabled controls visible.

Thanks!
 
Hi Mike,
You can create a "decoy" control that you size so small
that it is invisible and then set that as the focus when
you are hiding everything else that the user sees.
Below is a code sample where you have a decoy command
button (works well because they can be transparent as
well)called cmdDecoy and a command button that when
clicked either hides or unhides all the controls that
aren't labels on the form.

Private Sub Form_Open(Cancel As Integer)
fvisible = False
End Sub

Private Sub cmdHide_Click()
Dim ctl As Control
Me.cmdDecoy.SetFocus
For Each ctl In Me.Controls
If ctl.ControlType <> acLabel And ctl.Name
<> "cmdDecoy" And ctl.Name <> "cmdHide" Then
ctl.Visible = fvisible
End If
Next ctl
'module level variable that keeps track of if the
controls are hidden or not
'set this in the form's open event
fvisible = Not fvisible
End Sub

That is what I would suggest if you are hiding the
controls while executing code from within the current
form.
 
Back
Top