Up down arrow keys only work on list box after a mouse click...

  • Thread starter Thread starter RzB
  • Start date Start date
R

RzB

I have an unbound list box on a form. When the
form opens I run the following code...

'Make sure list box is enabled
Me.lstSelector.Enabled = True

'Select the top item in the list box
Me.lstSelector = Me.lstSelector.ItemData(0)

'Ensure list box has focus
Me.lstSelector.SetFocus

My problem is that the up down arrow keys
do not function on the list box. If I click on
an item in the list box with the mouse it works
just fine. Furthermore, once I have clicked with
the mouse the up down keys operate correctly...

How do I get the keys to work without having
to do a mouse click first?

Many thanks,
Roy
 
Is the listbox set for Multiselect Simple or Extended? If yes, you can't
just assign a value to it. You have to use the Selected property of the
listbox and set it to True for the desired row.

If the listbox isn't Multiselect, then you can set a value as you've done.
Which event are you doing this in? If you're doing this in the form's Open
event, it may be too soon, many parts of the form aren't ready at this
point. Try the Load event instead. I just tried this with a non Multiselect
listbox using the Load event and it worked as you say you want it to.
 
Wayne,
Many thanks for your help.
No it isn't multi select....
Here ia a picy of the list box properties..

http://www.gillandroy.com/accessprobs

Yes the code is in the form open event. I have moved
it to the load event but it makes no difference.

The form opens, the top items in the list box is highlighted
with a black background but the up/down arrow keys
won't work till I have clicked on one of the items in the
list box. It's almost as if it hasn't got focus till I click on it...
Hmmm...

Many thanks,
Roy
 
I didn't see a "header" row, did I miss one or is there not one? If there is
one, then Row(0) would be the header row and that may cause a problem.

You may want to make the SetFocus the last line (before Exit Sub or error
handlers) in the Load event. Also, is there any code running in a timer
event of this form or another open form?
 
Nope - no header row...

The set focus was the last line prior to the exit sub of the form load
handler. Apart from error handling code. But the same happens even when I
comment out the error handling code..

Interestingly I can't even tab off the list box until I've clicked with the
mouse...

No - No time code whatsoever...

Hmm..

Thanks for your help...
Roy
 
When tested here, it works. I wonder if the control is corrupted. Try
creating a new, blank form and put a couple of different controls on it,
including a listbox and try what you're doing there and see if it works. Put
in just enough code to select the row in the listbox and set focus to the
listbox.
 
Wayne the ability to set focus to a ListBox control in the Form's Load
event is not 100% reliable as the control is not always fully
instantiated at this point. Using a tool such as SPY++ you can see that
the ListBox window exists but it is still not visible. I realize you are
not seeing this behaviour on your machine but I have done extensive
testing with this issue.

Perhaps the OP could setfocus to an existing TextBox control and then
immediatedly setfocus to the ListBox control. I have seen this resolve
the issue in many cases.

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Thanks Stephen,

I hate timing issues. Thanks for the tip, I hadn't heard of that problem
before.
 
Stephen,

I got to thinking. Do you think it would work to set a flag in the Load
event then check for this flag in the Current event and set focus there,
then clear the flag? The published event order is Open ? Load ? Resize ?
Activate ? Current. Of course, Activate may work also if the user wants the
focus to return to this control every time they return to the form.
 
Guys, Just to let you know I am still looking at this - have been "out of
the office" earlier... I'm trying some of the things you have mentioned
above...

Many thanks for your help. I't really nice to have someone to bounce things
around with when you are working alone..

Perhaps I should have mentioned before that the form is a sub form on a
Tabbed form. When the tab is selected I set up the sub form by filling in
the SourceObject with the form name - thus causing it to burst into life...
When the form is out of site it is closed by removing the SourceObject.

I have just tried running the sub form on it's own (not selected from the
tabbed form) by double clicking on it in the database window, and it workd
correctly.. ie - the list box reacts to keyboard input ok.

So I'm currently crawling through the code starting from when the tab gets
selected to see what might have an effect.

Will report back later.

Roy
 
Yes, a subform would make a difference. I assume the code is in the
subform's event. This would set the focus to the control on the subform but
NOT set the focus to the subform itself. Whatever control on the main form
gets the initial focus will be the control with the focus and so you're
up/down arrow keys will try to act on it, not on the listbox in the subform.

To set the focus to a control on a subform is a two step process, first set
the focus to the subform control on the main form then set the focus to the
control on the subform. The subform control is a container control on the
main form. This is the control where you set the Parent/Child links. You
will need the name of this control, it may or may not be the same as the
name of the subform itself.

If the code is running on the main form:
Me.NameOfSubformControl.SetFocus
Me.NameOfSubformControl.Form.ctlControlOnSubform.SetFocus

If the code is running on the subform, change Me to Me.Parent in the first
line. You then should be able to set the focus to the listbox as normal
(Me.ctlOnSubform.SetFocus). This has the potential to have lots of timing
problems though since the main form and subform may not load in the order
desired. You may have to use the flag I mentioned in my previous post and
set the focus in the main form's Current event to give adequate time for
everything to be loaded.
 
Wayne,
Yes I had noticed that too... that's how I got to a fix...

Yes, Echo off helps a little but there is a flicker as soon as the tab
control gets clicked. I can get rid of that by putting the Echo off in the
On Exit of the subform control that is losing focus. That makes it look very
smooth. Unfortunately it's a nightmare keeping track of if the status and
I'm sure I would end up with Echo stuck off... Especially in erring
conditions. The user will have to put up with a little flicker :-)

Many thanks for your help,
Roy
 
Back
Top