Problem hiding a control

  • Thread starter John D in London, England
  • Start date
J

John D in London, England

I need to hide a control whenever it loses focus. I tried putting a hide
(i.e. .visibile = false) into its onlostfocus handler, but at run time it
complains that you cannot hide a control that has focus. So obviously even
though it is losing focus it actually still has focus when the lostfocus
event occurs.

Is there some other event where I should be putting the hide instruction?

The control (a list box) becomes visible and receives focus when the user
clicks on another specific control, and I want it to disappear whenever the
user clicks outside it. Any suggestions?
 
F

fredg

I need to hide a control whenever it loses focus. I tried putting a hide
(i.e. .visibile = false) into its onlostfocus handler, but at run time it
complains that you cannot hide a control that has focus. So obviously even
though it is losing focus it actually still has focus when the lostfocus
event occurs.

Is there some other event where I should be putting the hide instruction?

The control (a list box) becomes visible and receives focus when the user
clicks on another specific control, and I want it to disappear whenever the
user clicks outside it. Any suggestions?

Set focus anywhere else first.

Me![AnyOtherControlName].SetFocus
Me![ListBoxlName].Visible = False
 
J

John D in London, England

Thanks, Fred. That doesn't quite do it. I want the focus to go to where the
user clicked, rather than to a fixed place.

If the user clicks on a button is quite confusing if the cursor movers
somewhere cl
else and the button does not have its usual effect.

Is there any way that the LostFocus routine can discover where focus is
going to, so that it can issue its own SetFocus to the same place?

fredg said:
I need to hide a control whenever it loses focus. I tried putting a hide
(i.e. .visibile = false) into its onlostfocus handler, but at run time it
complains that you cannot hide a control that has focus. So obviously even
though it is losing focus it actually still has focus when the lostfocus
event occurs.

Is there some other event where I should be putting the hide instruction?

The control (a list box) becomes visible and receives focus when the user
clicks on another specific control, and I want it to disappear whenever the
user clicks outside it. Any suggestions?

Set focus anywhere else first.

Me![AnyOtherControlName].SetFocus
Me![ListBoxlName].Visible = False
 
H

Harry

You could try to script it in a way so that when a control gets the focus it
will hide the other control.
 
J

John D in London, England

But that would mean putting that into the gotfocus handler for all controls
(apart from 1) on the form. A lot of work there, and I'd have to be careful
not to break it by forgetting to include that for all new controls.
 
D

Dirk Goldgar

In John D in London, England
Thanks, Fred. That doesn't quite do it. I want the focus to go to
where the user clicked, rather than to a fixed place.

If the user clicks on a button is quite confusing if the cursor movers
somewhere cl
else and the button does not have its usual effect.

Is there any way that the LostFocus routine can discover where focus
is going to, so that it can issue its own SetFocus to the same place?

You can't tell where the focus is going, but if you really have to do
this you can use the form's Timer event to hide the list box. You might
set a module-level flag variable to indicate that the control should be
hidden, and you can avoid unnecessary overhead by only enabling the
Timer event when there's something for it to do. For example:

'----- start of example code -----
Dim mHideMyListbox As Boolean

Private Sub MyListbox_GotFocus()

mHideMyListbox = False

End Sub

Private Sub MyListbox_LostFocus()

mHideMyListbox = True
Me.TimerInterval = 10

End Sub

Private Sub Form_Timer()

If mHideMyListbox Then
Me.TimerInterval = 0
Me.MyListbox.Visible = False
End If

End Sub
'----- end of example code -----

If you don't think you'll ever need the form's Timer event for anything
else, you don't really need the flag variable at all. Just set the
TimerInterval to a nonzero value in the LostFocus event, and hide the
control and set the TimerInterval back to 0 in the Timer event.
 
J

John D in London, England

Thanks, Dirk, that is quite brilliant. It is exactly what I need.

As ususal the simplest ideas are the best.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top