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?
Thanks,
JT
jtran007Asked:
Who is Participating?
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++)
  {
         this.listCountry.SetSelected(x,false);
  }

Open in new window

0
HainKurtSr. System AnalystCommented:
try this

int ix = listCountry.SelectedIndex;
listCountry.SelectedItems = null;
listCountry.SelectedIndex = ix;
0
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.
Thanks,
JT
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Fab_CCommented:
Hi jtran007,

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

listCountry.ClearSelection();

I hope this helps,

Fab
0

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)
0
jtran007Author Commented:
Thanks,
JT
0
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.

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.