Discussion:
Continuously scroll through items in a drop down list
(too old to reply)
ArchieDog via OfficeKB.com
2006-08-10 10:26:26 UTC
Permalink
Hi All

I am trying to write what I'ms sure should be some simple little bit of code
to continuously loop through items in a dropdown list when the user presses
the up/down cursor keys. If the user is on the last item in a list then I
want the down cursor key to move to the first item; if on the first item then
the up cursor key should move to the last item in the list. In between it
should just move up/down as you would expect it to.

This is the code I have:

***********************************
Public Sub Scroll_Dropdown(cmbName As ComboBox, KeyCode)
'40 - cursor down
'38 - cursor up

'down cursor & list item is either blank or on the last item - select the
first item in the list
If (KeyCode = 40 And cmbName.ListIndex = -1) Or (KeyCode = 40 And cmbName.
ListIndex = cmbName.ListCount - 1) Then
cmbName.ListIndex = 0
'otherwise, if down cursor, add 1 to the current list index to select the
next item in the list
ElseIf KeyCode = 40 Then
cmbName.ListIndex = cmbName.ListIndex + 1
'up cursor and list item is either blank or on the first item in the list -
select the last item in the list
ElseIf (KeyCode = 38 And cmbName.ListIndex = -1) Or (KeyCode = 38 And cmbName.
ListIndex = 0) Then
cmbName.ListIndex = cmbName.ListCount - 1
'otherwise, if up cursor, subtract 1 from the current list index to select
the previous item
ElseIf KeyCode = 38 Then
cmbName.ListIndex = cmbName.ListIndex - 1
End If

End Sub
***********************************

The problem is that it sometimes jumps two items at a time. I'm sure there's
a simple explanation but I just can't see it.

Thanks in advance for any help.

ArchieD
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200608/1
Jezebel
2006-08-10 10:37:12 UTC
Permalink
All this work, to make your list work unexpectedly....
Post by ArchieDog via OfficeKB.com
Hi All
I am trying to write what I'ms sure should be some simple little bit of code
to continuously loop through items in a dropdown list when the user presses
the up/down cursor keys. If the user is on the last item in a list then I
want the down cursor key to move to the first item; if on the first item then
the up cursor key should move to the last item in the list. In between it
should just move up/down as you would expect it to.
***********************************
Public Sub Scroll_Dropdown(cmbName As ComboBox, KeyCode)
'40 - cursor down
'38 - cursor up
'down cursor & list item is either blank or on the last item - select the
first item in the list
If (KeyCode = 40 And cmbName.ListIndex = -1) Or (KeyCode = 40 And cmbName.
ListIndex = cmbName.ListCount - 1) Then
cmbName.ListIndex = 0
'otherwise, if down cursor, add 1 to the current list index to select the
next item in the list
ElseIf KeyCode = 40 Then
cmbName.ListIndex = cmbName.ListIndex + 1
'up cursor and list item is either blank or on the first item in the list -
select the last item in the list
ElseIf (KeyCode = 38 And cmbName.ListIndex = -1) Or (KeyCode = 38 And cmbName.
ListIndex = 0) Then
cmbName.ListIndex = cmbName.ListCount - 1
'otherwise, if up cursor, subtract 1 from the current list index to select
the previous item
ElseIf KeyCode = 38 Then
cmbName.ListIndex = cmbName.ListIndex - 1
End If
End Sub
***********************************
The problem is that it sometimes jumps two items at a time. I'm sure there's
a simple explanation but I just can't see it.
Thanks in advance for any help.
ArchieD
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200608/1
debbieprobert via OfficeKB.com
2006-08-10 10:50:50 UTC
Permalink
Yes, indeed. There are also exit events on some of the fields. If the user
cursors to the last item in a list & presses the down cursor again, then the
exit event will occur even if they were just pressing the cursor to see if
there was another item in the list. Although I can code around this the fact
that it happens is messy. In a perfect world, this wouldn't be needed but I
like to make the systems as easy and intuitive as possible. Besides, this is
not an unusual way for a list to behave.
Post by Jezebel
All this work, to make your list work unexpectedly....
Post by ArchieDog via OfficeKB.com
Hi All
[quoted text clipped - 47 lines]
Post by ArchieDog via OfficeKB.com
ArchieD
--
Message posted via http://www.officekb.com
Loading...