Deselect selected items in the list box

Hi Expert,
I have a list box with an item called "All", when user select "All" I highlight all items
in the list box :
                   if (country.Equals("All"))    // to select All countries
                        // listCountry.SelectedItems = listCountry.Items.Count;
                        for (int i = 0; i < this.listCountry.Items.Count; i++)
                            listCountry.SelectedIndex = i;
However, when user select again other item, all previous highlighted should be
no highlted. How can I do it?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:

  for (int x = 0; x < this.listCountry.Items.Count; x++)

Open in new window

HainKurtSr. System AnalystCommented:
try this

int ix = listCountry.SelectedIndex;
listCountry.SelectedItems = null;
listCountry.SelectedIndex = ix;
jtran007Author Commented:
Hi Expert,

After select all,

Roshmon: where do I put your code. it seems not working .

Guru: compiler complains.
liscountry.selecteditems = null   <---- read only can't change value
Please advise further.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Hi jtran007,

Listboxes have a .ClearSelection() function to get rid of the current selection, so you should be able to simply do :


I hope this helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roshan DavisCommented:
as Fab_C, try this.listCountry.ClearSelected(); before your selection code.
Please note the method is ClearSelected() (not ClearSelection)
jtran007Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.