Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2006

Tip: Looking for answers? Try searching our database.

keydown, arrow keys problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pointrider - 24 Mar 2006 03:19 GMT
I'm trying to use the keydown event to make the down arrow key change
the selected item in a listbox. The focus is in a texbox, and I've put
my code into the textbox_keydown event. The listbox selection is
changed, but then the focus is moved from the textbox to the next
control on the form. I don't want this... I want it to stay in the
textbox. How can I do this?

My event code is:

Private Sub NameBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
    If KeyCode = vbKeyDown Then
        Application.EnableEvents = False
        With NamesListBox
            If .ListIndex < .ListCount - 1 Then
                .ListIndex = .ListIndex + 1
            End If
        End With
        Application.EnableEvents = True
        Exit Sub
    End If
End Sub

Thanks for any help.
kletcho@gmail.com - 24 Mar 2006 07:35 GMT
Try at the end of your procedure:

Me.SetFocus
Pointrider - 24 Mar 2006 08:13 GMT
> Try at the end of your procedure:
>
> Me.SetFocus

Thanks for the suggestion... Tried it, but it gave me an error. Also
tried NameBox.SetFocus... no error, but it doesn't fix the behavior.
However, I've found that if I reset the keycode to zero before I exit
the NameBox_KeyDown event procedure, then all works as I wish. I guess a
keycode of 0 is ignored by whatever processes them downstream from me.
Kletcho - 24 Mar 2006 19:02 GMT
Doesn't the down arrow already move the selection down in any list box
if it is selected?  You should only have to set the tab order of the
form how you want it.  Then when the user tabs to the list box they can
use the down arrow.  To select the highlighted selection in the list
box you can push the space bar.  Thoughts?
Pointrider - 24 Mar 2006 22:13 GMT
> Doesn't the down arrow already move the selection down in any list box
> if it is selected?  You should only have to set the tab order of the
> form how you want it.  Then when the user tabs to the list box they can
> use the down arrow.  To select the highlighted selection in the list
> box you can push the space bar.  Thoughts?

You're right... the down arrow moves the selected row down (changes
ListIndex) in the listbox if the listbox is selected. But in my case,
the listbox isn't selected... a textbox is. And I want to leave the
textbox selected but have the down arrow change the ListItem property
for the listbox. The event handler I wrote for the textbox_keydown event
did that, but after the listbox was changed the focus moved from the
textbox to the next control on the form. I assume that what happened was
that after I processed the keycode, something in the system processed it
too, and moved the focus. By setting the keycode to 0 after I processed
it, the downstream handler apparently ignored it and I got the behavior
I wanted.

I can explain in more detail if you want it, but setting the keycode
seems to be the solution.

Thanks for the responses...
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.