次の方法で共有


ListBox.Sorted プロパティ

ListBox 内の項目をアルファベット順に並べ替えるかどうかを示す値を取得または設定します。

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

プロパティ値

コントロール内の項目を並べ替える場合は true 。それ以外の場合は false 。既定値は false です。

解説

このプロパティを使用すると、 ListBox 内の項目が自動的にアルファベット順に並べ替えられます。並べ替え済みの ListBox に項目を追加すると、追加された項目は並べ替え済みのリスト内で適切な位置に挿入されます。項目を ListBox に追加するときは、既存の項目を並べ替えてから新しい項目を追加した方が効率的です。

使用例

[Visual Basic, C#, C++] GetSelected メソッドを使用して ListBox 内のどの項目が選択されているかを判別し、現在選択されていない項目を選択して、現在選択されている項目の選択を解除する方法を次の例に示します。この例では、 SelectionMode プロパティを使用して ListBox で複数の項目の選択を許可する方法と、 Sorted プロパティを使用して ListBox 内の項目を自動的に並べ替える方法についても示します。この例は、 listBox1 という名前の ListBox がフォームに追加されていて、この例で定義されている InitializeMyListBox メソッドがフォームの Load イベントから呼び出されることを前提にしています。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ListBox クラス | ListBox メンバ | System.Windows.Forms 名前空間