Export (0) Print
Expand All

ListBox.Sorted Property

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

[Visual Basic]
Public Property Sorted As Boolean
[C#]
public bool Sorted {get; set;}
[C++]
public: __property bool get_Sorted();
public: __property void set_Sorted(bool);
[JScript]
public function get Sorted() : Boolean;
public function set Sorted(Boolean);

Property Value

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

Remarks

You can use this property to automatically sort items 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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Private Sub 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
End Sub

Private Sub InvertMySelection()

   Dim x As Integer
   ' Loop through all items the ListBox.
   For x = 0 To listBox1.Items.Count - 1

      ' Determine if the item is selected.
      If listBox1.GetSelected(x) = True Then
         ' Deselect all items that are selected.
         listBox1.SetSelected(x, False)
      Else
         ' Select all items that are not selected.
         listBox1.SetSelected(x, True)
      End If
   Next x
   ' Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex = 0
End Sub

[C#] 
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;
}

[C++] 
private:
   void InitializeMyListBox()
   {
      // Add items to the ListBox.
      listBox1->Items->Add(S"A");
      listBox1->Items->Add(S"C");
      listBox1->Items->Add(S"E");
      listBox1->Items->Add(S"F");
      listBox1->Items->Add(S"G");
      listBox1->Items->Add(S"D");
      listBox1->Items->Add(S"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;
   }

   void InvertMySelection()
   {
      // Loop through all items the ListBox.
      for (int x = 0; x < listBox1->Items->Count; x++)
      {
         // Select all items that are not selected,
         // deselect all items that are selected.
         listBox1->SetSelected(x, !listBox1->GetSelected(x));      
      }
      // Force the ListBox to scroll back to the top of the list.
      listBox1->TopIndex=0;
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ListBox Class | ListBox Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft