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 / Word / Programming / August 2006

Tip: Looking for answers? Try searching our database.

Continuously scroll through items in a drop down list - resubmit

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DebsP - 10 Aug 2006 12:57 GMT
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
Doug Robbins - Word MVP - 10 Aug 2006 18:57 GMT
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

Signature

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

> Hi All
>
[quoted text clipped - 54 lines]
>
> ArchieD
ArchieD - 21 Aug 2006 15:48 GMT
Thanks very much, Doug.

>Here's a method of using a spin button to move the selected item either up
>or
[quoted text clipped - 40 lines]
>>
>> ArchieD
 
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.