SelectionMode Enumeration
Specifies the selection behavior of a list box.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
This enumeration is used by classes such as ListBox and CheckedListBox.
The following 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 assumes 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. listBox1.Items.Add("A"); listBox1.Items.Add("C"); listBox1.Items.Add("E"); listBox1.Items.Add("F"); listBox1.Items.Add("G"); listBox1.Items.Add("D"); listBox1.Items.Add("B"); // 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. listBox1.SetSelected(0,true); listBox1.SetSelected(2,true); listBox1.SetSelected(4,true); // Force the ListBox to scroll back to the top of the list. listBox1.TopIndex=0; } 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. listBox1.SetSelected(x,false); else // Select all items that are not selected. listBox1.SetSelected(x,true); } // Force the ListBox to scroll back to the top of the list. listBox1.TopIndex=0; }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.