Hiding a control After it lost focus

  • Thread starter Thread starter dhstein
  • Start date Start date
D

dhstein

This should be a fairly simple and I would think a fairly common situation.
I have a control - let's call it "Control #1". The user clicks Control #1
and another control (Control #2) becomes visible. He uses the control #2
(combo box, text box, whatever) and when he's done he clicks another control
(Control #3) to do something else. I'd like to hide Control #2 at this
point.
Problem 1 If I use the lost focus event of Control # 2 I can't hide it.
Problem 2 If I set focus to some control (Control #99) then the user who
clicked Control #3 is now sitting on Control 99 - that's confusing.

The cumbersome way is to go to every control on the form and the first thing
it does is hide Control #2. I have a lot of controls and I would think there
is a better way. Anybody have a better way? Thanks.
 
I'm curious to see what responses your post triggers ... from my limited
experience I'm not certain it's trivial.

What I have done in what seems to be perhaps a similiar situation is
change the font color of Control #2 from red (function not available) to
green (function available) ... I think that can be done in the lost
focus event. I've also read here that the font (and border?) can be set
to the background color which effectively hides the control.

HTH!
 
Try creating a function like:

Function HidePreviousControl()
On Error GoTo ErrHandler

Dim ctlPrev As Control

Set ctlPrev = Screen.PreviousControl
If ctlPrev.Name <> Screen.ActiveControl.Name Then
ctlPrev.Visible = False
End If

ExitHere:
Set ctlPrev = Nothing
Exit Function

ErrHandler:
Select Case Err.Number
Case 2467, 2483 ' "No previous control"
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
Resume ExitHere

End Function

Call that function in the GotFocus of each control.
 
Back
Top