ListBox.Sorted Vlastnost

Definice

Získá nebo nastaví hodnotu označující, zda jsou položky v objektu ListBox seřazeny abecedně.

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

Hodnota vlastnosti

truepokud jsou položky v ovládacím prvku seřazeny; v opačném případě . false Výchozí formát je false.

Příklady

Následující příklad kódu ukazuje, jak pomocí GetSelected metody určit, které položky v objektu ListBox jsou vybrány, aby bylo možné vybrat položky, které nejsou vybrány, a zrušit výběr vybraných položek. Příklad také ukazuje použití SelectionMode vlastnosti k povolení více než jednu vybranou ListBox položku a používá Sorted vlastnost k předvedení, jak automaticky seřadit položky v objektu ListBox . Tento příklad vyžaduje, aby ListBoxbyl do formuláře přidán objekt s názvem listBox1a aby InitializeMyListBox byla metoda definovaná v příkladu volána z Load události formuláře.

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

Poznámky

Sorted Pomocí vlastnosti můžete automaticky řadit řetězce abecedně v objektu ListBox. Při přidávání položek do seřazeného ListBoxseznamu se položky přesunou do příslušného umístění v seřazeného seznamu. Při přidávání položek do ListBoxje efektivnější položky nejprve seřadit a pak přidat nové položky.

Objekt ListBox s nastavenou Sorted na true hodnotu by neměl být svázán s daty pomocí DataSource vlastnosti . Pokud chcete zobrazit seřazená data ve vázaném ListBoxobjektu , měli byste vytvořit vazbu ke zdroji dat, který podporuje řazení, a nechat zdroj dat poskytovat řazení.

Platí pro