ListBox.Sorted Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets a value indicating whether the items in the ListBox are sorted alphabetically.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public bool Sorted { get; set; }

Property Value

Type: System.Boolean

true if items in the control are sorted; otherwise, false. The default is false.

Use the Sorted property to automatically sort strings alphabetically in a ListBox. As items are added to a sorted ListBox, the items are moved to the appropriate location in the sorted list. When adding items to a ListBox, it is more efficient to sort the items first and then add new items.

A ListBox with its Sorted set to true should not be bound to data using the DataSource property. To display sorted data in a bound ListBox, you should bind to a data source that supports sorting and have the data source provide the sorting.

The following code example demonstrates how to use the GetSelected method to determine which items in a ListBox are selected in order to select the items that are not selected and deselect the items that are selected. The example also demonstrates using the SelectionMode property to enable a ListBox to have more than one selected item and uses the Sorted property to demonstrate how to sort items in a ListBox automatically. This example requires that a ListBox, named listBox1, has been added to a form and that the InitializeMyListBox method defined in the example is called from the Load event of the form.

private void InitializeMyListBox()
   // Add items to the ListBox.

   // Sort all items added previously.
   listBox1.Sorted = true;

   // Set the SelectionMode to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;

   // Select three initial items from the list.

   // Force the ListBox to scroll back to the top of the list.

private void InvertMySelection()
   // Loop through all items the ListBox.
   for (int x = 0; x < listBox1.Items.Count; x++)
      // Determine if the item is selected.
      if(listBox1.GetSelected(x) == true)
         // Deselect all items that are selected.
         // Select all items that are not selected.
   // Force the ListBox to scroll back to the top of the list.

.NET Framework
Available since 1.1
Return to top