Freigeben über


ListBox.Sorted Eigenschaft

Definition

Dient zum Abrufen oder Festlegen eines Werts, der angibt, ob die Elemente in der ListBox Zeichenfolge alphabetisch sortiert sind.

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

Eigenschaftswert

truewenn Elemente im Steuerelement sortiert sind; andernfalls . false Der Standardwert lautet false.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die GetSelected Methode verwendet wird, um zu bestimmen, welche Elemente in einer ListBox Auswahl ausgewählt sind, um die nicht ausgewählten Elemente auszuwählen und die ausgewählten Elemente zu deaktivieren. Im Beispiel wird auch die Verwendung der SelectionMode Eigenschaft veranschaulicht, um ein ListBox ausgewähltes Element zu aktivieren und die Sorted Eigenschaft zu verwenden, um zu veranschaulichen, wie Elemente in einem ListBox automatischen Element sortiert werden. Dieses Beispiel erfordert, dass ein ListBoxbenanntes listBox1Formular einem Formular hinzugefügt wurde und dass die InitializeMyListBox im Beispiel definierte Methode aus dem Load Ereignis des Formulars aufgerufen wird.

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))
         // 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

Hinweise

Verwenden Sie die Sorted Eigenschaft, um Zeichenfolgen alphabetisch in einem ListBox. Wenn Elemente zu einer sortierten ListBoxListe hinzugefügt werden, werden die Elemente an die entsprechende Position in der sortierten Liste verschoben. Beim Hinzufügen von Elementen zu einem ListBoxElement ist es effizienter, die Elemente zuerst zu sortieren und dann neue Elemente hinzuzufügen.

A ListBox mit seiner Sorted Gruppe true sollte nicht an Daten gebunden werden, die die DataSource Eigenschaft verwenden. Um sortierte Daten in einer gebundenen ListBoxDaten anzuzeigen, sollten Sie eine Bindung an eine Datenquelle erstellen, die das Sortieren unterstützt und die Datenquelle die Sortierung bereitstellt.

Gilt für: