Link to home
Start Free TrialLog in
Avatar of Karl001
Karl001Flag for Canada

asked on

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

How to clear selected items in a multi-select listbox?
Avatar of Lokesh B R
Lokesh B R
Flag of India image

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

ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Karl001

ASKER

Thanks
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
Avatar of Karl001

ASKER

Thanks very much