How to clear selected items in a multi-select listbox?

How to clear selected items in a multi-select listbox?
Karl001Asked:
Who is Participating?
 
Dale FyeCommented:
You need to loop through the SelectedItems collection; something like:

Dim varItem as Variant

    For Each varItem In Me.lst_YourListName.ItemsSelected
   
        Me.lst_YourListName.Selected(varItem) = False
       
    Next
0
 
Lokesh B RDeveloperCommented:
Hi,

You want to remove the selected items from ListBox?

here is the code.


   
 List<string> selectedItems = new List<string>();
            foreach (ListItem item in ListBox1.Items)
            {
                if (item.Selected)
                {
                    selectedItems.Add(item.Value);
                }
            }

            foreach (string item in selectedItems)
            {
                ListBox1.Items.Remove(item);
            }

Open in new window

0
 
Karl001Author Commented:
Thanks
0
 
Dale FyeCommented:
Glad to help.  

I actually have a subroutine that sits in a standard code module which I use to clear all selections in a listbox.  It makes it easier to do this from all forms with a single line of code:

Public Sub MultiListClearSelections(lst as List)

    Dim varItem as Variant

    for each varItem in lst.itemsselected

        lst.Selected(varItem) = False

    Next

End Sub

Open in new window

This makes it easy to call this from a button click on any form:

MultiListClearSelections me.lst_YourListName
0
 
Karl001Author Commented:
Thanks very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.