Select *all* items in listbox

  • Thread starter Thread starter RD
  • Start date Start date
R

RD

Hi all,

I want to be able to select *all* the items of a listbox. Is there a way to do
this with a single statement or do I have to loop through it using the ListCount
property?

Thanks,
RD
 
If you want to add a button that will let your users Select/Deselect All the
following code will do this:

Private Sub cmdSelectAll_Click()
Dim n As Integer, i As Integer
If Me.cmdSelectAll.Caption = "Select All" Then
i = 1
Me.cmdSelectAll.Caption = "De-Select All"
Else
i = 0
Me.cmdSelectAll.Caption = "Select All"
End If
For n = 0 To Me.lstMyList.ListCount - 1
Me.lstMyList.Selected(n) = i
Next n
End Sub
 
You need to loop.

Yeah, I was afraid of that. I've already written the loop, I was just wondering
if there wasn't something like a Me.listbox.SelectAll kind of thing.

Oh well.

Thanks for the response.

Regards,
RD
 
Create a generic function (in a module, not associated with a form), and
call it whenever you need it.

Sub ListboxSelectAll(WhatList As ListBox)

Dim intCurrentRow As Integer

For intCurrentRow = 0 To WhatList.Listcount - 1
WhatList.Selected(intCurrentRow) = True
Next intCurrentRow

End Sub

In your code, you'd use something

Call ListboxSelectAll Me!MyListBox
 
Create a generic function (in a module, not associated with a form), and
call it whenever you need it.

Sub ListboxSelectAll(WhatList As ListBox)

Dim intCurrentRow As Integer

For intCurrentRow = 0 To WhatList.Listcount - 1
WhatList.Selected(intCurrentRow) = True
Next intCurrentRow

End Sub

In your code, you'd use something

Call ListboxSelectAll Me!MyListBox

Excellent! A new item for my library.

Thanks,
RD
 
Back
Top