Log in

View Full Version : Clear Mulit-Select Listbox



gmaxey
01-29-2013, 03:05 PM
Not that it will make a whit of difference in most cases, I was asked if there was a way to avoid looping through all the list entries in a mulit-select listbox to reset or clear the box.

I couldn't find something as simple as "oLB.Clear or oLB.Reset" but I did find that if I change the type to single and use .ListIndex = -1 and then change the type back that the loop is avoided.

To clear a listbox, pass it to this function:

Function ClearLB(ByRef oLB As Msforms.ListBox) As Boolean
Dim lngType As Long
lngType = oLB.MultiSelect
oLB.MultiSelect = fmMultiSelectSingle
oLB.ListIndex = -1
oLB.MultiSelect = lngType
ClearLB = True
End Function

Anybody aware of or see any problems with this approach?

fumei
01-30-2013, 04:35 PM
No. It is quite clever actually. Although you can do it directly with:Private Sub CommandButton1_Click()
ListBox1.MultiSelect = 0
ListBox1.MultiSelect = 1
End Sub
Assuming the commandbutton purpose is to clear the selected items.

It also assumes the original value was not extended. In which case of course your method is better as it replaces the original.

fumei
01-30-2013, 04:41 PM
Although I am not sure you need ListIndex = -1
Dim lngType As Long
lngType = ListBox1.MultiSelect
ListBox1.MultiSelect = 0
ListBox1.MultiSelect = lngTypeseems to work for me. For either Extended or basic Multi.

gmaxey
02-01-2013, 10:08 AM
Gerry,

Thanks.