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
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