Link to home
Start Free TrialLog in
Avatar of fabi2004
fabi2004Flag for United States of America

asked on

How to hide a listbox control (on a form) when the mouse moves away from the control

I have a button on a form that, when clicked, makes a list box visible.  (The feature is meant to simulate a drop-down menu or sub menu.)

I have two questions although the first is the important one.

1.  How do I make the list box control become invisible when the mouse moves away from it?
I've tried,  the on-click event, the lost-focus event, the mouse-move event.  Nothing seems to work quite right.  I understand I have to move the focus to another control before I can make the current one invisible, but even this errors out most of the time.

2. Is there a way to "highlight" each list box option as the mouse moves over it?
Again, simulating a drop-down menu feature.  

More info:
Using the After Update event, each of my list box options opens an individual form when it's clicked.
Once the an option in the list box is chosen, the list box should become invisible and the correct form should open.  
     Right now I have the form opening, the focus moving to another control on the form and the listbox visible property setting to false. It errors out when the focus tries to go to another control.
Avatar of Anders Ebro (Microsoft MVP)
Anders Ebro (Microsoft MVP)
Flag of Denmark image

Well the first question that comes to mind, is why not just simply use a dropdown?


There is no "mouseaway" event, and the closest you can get is to add a mousemove to all other controls and the form sections as well, in which you hide the listbox. It can be a bit clumsy.

As for 2), I don't really think so.
No points please.

As a minimum, you need to select the form object and use the mousemove event of the form to hide your other control.  But if you move your mouse too quickly to another control on the form, then the Form_MouseMove event may not fire to hide your listbox.  Because of this, Anders recommendation to put similar code in mousemove event of the other controls as well as the form, might help.
Avatar of fabi2004

ASKER

I'd be happy if I could hide the list box on lost-focus or even on the on-click event.  It doesn't have to be on mouse-move.  In fact, mouse-move won't work because when a user moves from one list option to another, the entire list box disappears.

When the after-update event executes and opens a form to a new record, the focus is on the new form correct?  So, I should be able to hide the control on the previous form which remains open in the background?  Or, maybe I need to set focus on a control on the newly opened form first?
Maybe it will help if I post some of the code I've used that doesn't work to give you an idea of what I'm trying to do:

When the form loads, I hide the list box

Private Sub Form_Load()
    DoCmd.NavigateTo "acNavigationCategoryObjectType"
    DoCmd.Minimize
   Me.lstForms.Visible = False
End Sub

Open in new window


when a command button is clicked, the list box is revealed. works great

Private Sub cmdAddEdit_Click()
   Me.lstForms.Visible = True
End Sub

Open in new window


To hide the list box, I've tried the lost-focus event, but it only seems to work sporadically.  I'm not sure it's doing anything at all.

Private Sub lstForms_LostFocus()
    Me.cmdAddEdit.SetFocus
    Me.lstForms.Visible = False
End Sub

Open in new window


I've tried to hide the list box as part of it's After Update Event which I'm using to allow users a choice of which form to open.  Please note I've tried variations of code on each Case as testing.  None of them work without throwing an error.  The error is either "can't set focus to another control" or it's "can't hide a control with focus".

Private Sub lstForms_AfterUpdate()

Select Case Me.lstForms.Value
Case "Employees"
   Me!cmdAddEdit.SetFocus
   Me.lstForms.Visible = False
   DoCmd.OpenForm "frmEmployees"
   DoCmd.GoToRecord , , acNewRec
Case "Clients"
   DoCmd.OpenForm "frmClients"
   DoCmd.GoToRecord , , acNewRec
   Me!cmdOpenScheduler.SetFocus
   Me.lstForms.Visible = False
Case "Providers"
   DoCmd.OpenForm "frmProviders"
   DoCmd.GoToRecord , , acNewRec
   Forms("frmSplashScreen").lstForms.Visible = False
End Select

End Sub

Open in new window


I'm thinking of trying to hide it on the on-click even of the details part of the form.  But I think I'd also have to hide it on the on-click of the form header, and on-click of every other command button on the form, just to cover the bases of where a user might click next.  Of course, nothing will happen if they just move they mouse away, but I can live with that if it's not an option as you guys stated above.
The concern here is that whatever way you end up doing, ..and for all its complexity,
 "add a mousemove to all other controls and the form sections as well"
...and look at all the complex code you have already tried, ...

... this may never be "perfect"

As Anders stated, ...there is no "Mouse Away" event.

And remember that many users do not even use the mouse, ....what then?

So we may have to go back to the question of why this non-standard functionality is even needed...?

Perhaps there is a more elegant way to do this...

JeffCoachman
What is about the combobox that you dislike? There are alot of settings for the combobox that can be changed. It too can be multicolumn, the width of each column can be adjusted, and the amount of items visible on dropdown can be customized.
Alright, 3 strikes and I'm out.  I guess i was trying to do something in a way that isn't efficient or effective in an Access form.

Thank you all for the feedback and support.

Thank you Anders for being the first to respond.  As far as the combo box, I don't like that there is an arrow to click in order to open it.  It's that simple.  It just doesn't fit with the design of my form.  I didn't realize I was beating my head against a wall trying to do this a different way.  Thanks again!
I didn't say it couldn't be done, I was just curios as to the reasons behind. A thing I dislike about the listbox is that it doesn't auto-resize depending on whether you have 5 items or 3.  It just seemed like you went to alot of trouble to making a listbox look and feel like a combo.

I played around with it a bit. The key issue is that even though you open up a new form, within the main/menu form, that control is still the control that has focus for THAT form. So one solution might be to set focus to the commandbutton you use to dropdown the list, before you hide the listbox.

I put a demo together for you. Have a look and see if you like it. One thing I disliked was trying to navigate the listbox with the keyboard would cause the forms to open. I don't have a fix for that right now.
DemoList.zip
Anders, that was one of the first things I tried.  In my db, it errors out.

your code

Private Sub List1_AfterUpdate()
    DoCmd.OpenForm List1.Value
    Me.cmdFrmSelector.SetFocus
    Me.List1.Visible = False
End Sub

Open in new window


my code

Select Case Me.lstForms.Value
Case "Employees"
   DoCmd.OpenForm "frmEmployees"
   DoCmd.GoToRecord , , acNewRec
   Me.cmdAddEdit.SetFocus
   Me.lstForms.Visible = False
Case...
End Select

Open in new window


Result:  Runtime error 2110.  Microsoft Access can't move the focus to the control cmdAddEdit.

Here's a picture of my form with the logo and name removed.  It works similar to a navigation form.  It is not a tabbed subform.  Those are buttons that allow a user to open other forms/reports which open up new Access "tabs" across the top of the screen and are able to use the full width of the screen.

I'm ok with the idea that this can't/shouldn't be done the way I've been trying.  Sometimes it's easier to find another route than to navigate all the potholes in the current one.
Home.jpg
Is the code you just showed also in the AfterUpdate event?

Try moving the Me.cmdAddEdit.SetFocus to before opening the form and see if that makes a difference.
Yes and already tried.  It's the first case in the code I posted earlier. (tried with a ! and . ) not sure what difference either makes but neither works.  I even tried moving the focus to a different control.

Private Sub lstForms_AfterUpdate()

Select Case Me.lstForms.Value
Case "Employees"
   Me!cmdAddEdit.SetFocus
   Me.lstForms.Visible = False
   DoCmd.OpenForm "frmEmployees"
   DoCmd.GoToRecord , , acNewRec
Case "Clients"
   DoCmd.OpenForm "frmClients"
   DoCmd.GoToRecord , , acNewRec
   Me!cmdOpenScheduler.SetFocus
   Me.lstForms.Visible = False
Case "Providers"
   DoCmd.OpenForm "frmProviders"
   DoCmd.GoToRecord , , acNewRec
   Forms("frmSplashScreen").lstForms.Visible = False
End Select

End Sub

Open in new window

Hmm. if you want, you can email me the database and I can take a look at it. I can't think of anything else right now.
Wow!  That would be awesome.  But don't spend too much time on it.  Next week, if I haven't found a way to make this work, I'll give it a shot with a combo box control or a mini subform or something else.

I was trying to make that home page look and feel sort of like working in a browser on a website.
ASKER CERTIFIED SOLUTION
Avatar of Anders Ebro (Microsoft MVP)
Anders Ebro (Microsoft MVP)
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You found that in like 2 seconds flat!!!  I spent an entire day rewriting a dozen words of code.  I can't thank you enough for your help.  Very much appreciated!