Unselect all items in a listbox after initial load

69,539

Solution 1

ListBox.ClearSelected()

or

ListBox.SelectedIndex = -1

Of course, they are member methods.

Solution 2

C# WPF (Clear Multiple)

Two more ways from code behind:

DemoListBox.SelectedItems.Clear();
DemoListBox.UnselectAll()

Solution 3

This should do it:

lb_MyListBox.SelectedIndex = -1;

Solution 4

For clear multiple ListBox

foreach (Control ctrl in ctrls)
{
    ((ListBox)ctrl).SelectedIndex = -1;
    ClearInputs(ctrl.Controls);
}

Calling Method on button and from where you want to execute

ClearInputs(Page.Controls);
Share:
69,539
Jason Rae
Author by

Jason Rae

Updated on July 22, 2022

Comments

  • Jason Rae
    Jason Rae almost 2 years

    I have a ListBox that initially displays with the first item selected. I want it to display with no items selected. Is this possible?

  • kakridge
    kakridge almost 13 years
    SelectedIndex = -1 will not work if you have a multi-edit listbox.
  • bonCodigo
    bonCodigo almost 10 years
    @Vladimir Does it work for a multi-value extended listbox as well? In my case this code doesn't work.
  • bonCodigo
    bonCodigo almost 10 years
    @kakridge precisely. I am facing the issue for mult-selct extend. What's the workaround?
  • Peet
    Peet over 5 years
    The first one worked a treat for my CheckedListBox. Thanks!
  • Leo
    Leo about 4 years
    ListBox.ClearSelection();