ListBox.Sorted Właściwość

Definicja

Pobiera lub ustawia wartość wskazującą, czy elementy w obiekcie ListBox są sortowane alfabetycznie.

public:
 property bool Sorted { bool get(); void set(bool value); };
public bool Sorted { get; set; }
member this.Sorted : bool with get, set
Public Property Sorted As Boolean

Wartość właściwości

true jeśli elementy w kontrolce są sortowane; w przeciwnym razie , false. Wartość domyślna to false.

Przykłady

W poniższym przykładzie kodu pokazano, jak za pomocą GetSelected metody określić, które elementy w obiekcie ListBox są wybrane, aby wybrać elementy, które nie są zaznaczone, i usunąć zaznaczenie zaznaczonych elementów. W przykładzie pokazano również użycie SelectionMode właściwości , aby umożliwić ListBox elementowi więcej niż jeden wybrany element i używa Sorted właściwości w celu pokazania, jak automatycznie sortować elementy w elemencie ListBox . Ten przykład wymaga dodania ListBoxobiektu o nazwie listBox1do formularza i wywołania InitializeMyListBox metody zdefiniowanej w przykładzie ze Load zdarzenia formularza.

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;
   }

   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 ) );
      }
      listBox1->TopIndex = 0;
   }
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;
}
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

Uwagi

Użyj właściwości , Sorted aby automatycznie sortować ciągi alfabetycznie w obiekcie ListBox. W miarę dodawania elementów do posortowanej ListBoxelementy są przenoszone do odpowiedniej lokalizacji na posortowanej liście. Podczas dodawania elementów do elementu ListBoxjest bardziej wydajne sortowanie elementów, a następnie dodawanie nowych elementów.

Element ListBox z ustawionym Sorted wartością true nie powinien być powiązany z danymi przy użyciu DataSource właściwości . Aby wyświetlić posortowane dane w powiązanej ramce ListBox, należy powiązać ze źródłem danych obsługującym sortowanie i udostępnić źródło danych.

Dotyczy