How to create a way of shift-clicking in a list to select a range

  • Thread starter Thread starter kelly d via AccessMonster.com
  • Start date Start date
K

kelly d via AccessMonster.com

This is a little tip i figured out on how to select a range of items in a
listbox by clicking on a start item and pressing shift then clicking on
another item and having all the items inbetween select too. (kinda like in
windows explorer)
I figured I'd offer this up to anybody wanting to know but havn't already
figured it out.

first, make sure your listbox's multi-select property is set to simple or
extended. ( i prefer extended with this one cuz it lets my users select a
range, use control-click to select individual items, and still be able to
plain click to undo all range selections and control-click selections)

2nd, we need a couple of public variables in the general declarations of the
module:

Public Ushift As Integer, ListStart As Integer 'Ushift holds the state
of the shift key and ListStart holds the beginning value of the range

3rd, in the listbox's OnKeyDown event:

Ushift=shift

4th, in the listbox's OnKeyUp event:

Ushift=0

5th, in the listbox's AfterUpdate event:

If Ushift = 0 Then
ListStart = NameOfYourListBox.ListIndex 'if shift key is up then
what you just clicked on is the starting point

End If
If Ushift = 1 Then 'if shift key is down
If ListStart <> NameOfYourListBox.ListIndex Then ' if you try
running a range when the start and end are the same, the loop ends up being
continuous
If NameOfYourListBox.ListIndex < start Then stp = -1 ' if you
clicked an item above your start point then range goes up the list
If NameOfYourListBox.ListIndex > start Then stp = 1 'if you
clicked an item below your start point then range goes down the list

For rng = ListStart To NameOfYourListBox.ListIndex Step stp
'start the range selecting going up or going down
NameOfYourListBox.Selected(rng) = True 'selecting an item in
the list
Next rng

End If
End If
ListStart = NameOfYourListBox.ListIndex ' set the range start point to
the end point in the range we just filled
 
It wasnt until after I hit 'post' that I noticed in my code, everywhere it
references a variable namded start, it should be referencing a variable named
ListStart. sorry. since one cant edit ones posts, please make note of this
reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top