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

Apologies for re-submitting this. Am worried it won't get answered as had a
response that was an observation rather than an answer. Rather keen to get
the latter & not sure if anyone else would pick it up.

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
Doug Robbins - Word MVP
2006-08-10 17:57:20 UTC
Permalink
Here's a method of using a spin button to move the selected item either up
or
down through the list of items in a listbox on a userform:

Option Explicit
Dim i As Integer, Temp As Variant

Private Sub SpinButton1_SpinDown()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
If i = ListBox1.ListCount - 1 Then
Exit Sub
End If
Temp = ListBox1.List(i + 1)
ListBox1.List(i + 1) = ListBox1.List(i)
ListBox1.List(i) = Temp
ListBox1.ListIndex = i + 1
Exit For
End If
Next i

End Sub

Private Sub SpinButton1_SpinUp()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
If i = 0 Then
Exit Sub
End If
Temp = ListBox1.List(i - 1)
ListBox1.List(i - 1) = ListBox1.List(i)
ListBox1.List(i) = Temp
ListBox1.ListIndex = i - 1
Exit For
End If
Next i

End Sub
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
Post by DebsP via OfficeKB.com
Hi All
Apologies for re-submitting this. Am worried it won't get answered as had a
response that was an observation rather than an answer. Rather keen to get
the latter & not sure if anyone else would pick it up.
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
ArchieD via OfficeKB.com
2006-08-21 14:48:22 UTC
Permalink
Thanks very much, Doug.
Post by Doug Robbins - Word MVP
Here's a method of using a spin button to move the selected item either up
or
Option Explicit
Dim i As Integer, Temp As Variant
Private Sub SpinButton1_SpinDown()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
If i = ListBox1.ListCount - 1 Then
Exit Sub
End If
Temp = ListBox1.List(i + 1)
ListBox1.List(i + 1) = ListBox1.List(i)
ListBox1.List(i) = Temp
ListBox1.ListIndex = i + 1
Exit For
End If
Next i
End Sub
Private Sub SpinButton1_SpinUp()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
If i = 0 Then
Exit Sub
End If
Temp = ListBox1.List(i - 1)
ListBox1.List(i - 1) = ListBox1.List(i)
ListBox1.List(i) = Temp
ListBox1.ListIndex = i - 1
Exit For
End If
Next i
End Sub
Post by DebsP via OfficeKB.com
Hi All
[quoted text clipped - 54 lines]
Post by DebsP via OfficeKB.com
ArchieD
--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200608/1
Loading...