ListBox.Sorted ListBox.Sorted ListBox.Sorted ListBox.Sorted Property

定義

ListBox 内の項目をアルファベット順に並べ替えるかどうかを示す値を取得または設定します。 Gets or sets a value indicating whether the items in the ListBox are sorted alphabetically.

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

プロパティ値

コントロール内の項目を並べ替える場合は true。それ以外の場合は false true if items in the control are sorted; otherwise, false. 既定値は、false です。 The default is false.

次のコード例は、使用する方法を示します、GetSelectedでアイテムを決定する方法、ListBoxが選択されていない項目を選択し、選択されている項目の選択を解除するために選択します。The following code 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. 使用しても示します、SelectionModeプロパティを有効にする、ListBoxが 1 つ以上使用して項目を選択、Sortedプロパティ内の項目を並べ替える方法を示すために、ListBox自動的にします。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. この例では、する必要があります、 ListBox、名前付きlistBox1、フォームに追加されましたが、InitializeMyListBoxの例で定義されているメソッドの呼び出し元、Loadフォームのイベント。This example requires 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;
   }

   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

注釈

使用して、Sortedを自動的に並べ替えるプロパティの文字列でアルファベット順に、ListBoxします。Use the Sorted property to automatically sort strings alphabetically in a ListBox. 項目が追加されると、並べ替えにListBoxアイテムが並べ替えられたリスト内の適切な場所に移動します。As items are added to a sorted ListBox, the items are moved to the appropriate location in the sorted list. 項目を追加するときに、 ListBox、最初の項目の並べ替えし、新しい項目を追加する方が効率的です。When adding items to a ListBox, it is more efficient to sort the items first and then add new items.

AListBoxでそのSorted設定trueを使用してデータにバインドされていません、DataSourceプロパティ。A ListBox with its Sorted set to true should not be bound to data using the DataSource property. バインドされたデータを並べ替えられて表示ListBox、並べ替えをサポートするデータ ソースにバインドし、データ ソースの並べ替えを指定する必要があります。To display sorted data in a bound ListBox, you should bind to a data source that supports sorting and have the data source provide the sorting.

適用対象