ListBox.SelectionMode プロパティ

定義

ListBox で項目を選択する方法を取得または設定します。

public:
 virtual property System::Windows::Forms::SelectionMode SelectionMode { System::Windows::Forms::SelectionMode get(); void set(System::Windows::Forms::SelectionMode value); };
public virtual System.Windows.Forms.SelectionMode SelectionMode { get; set; }
member this.SelectionMode : System.Windows.Forms.SelectionMode with get, set
Public Overridable Property SelectionMode As SelectionMode

プロパティ値

SelectionMode 値のいずれか 1 つ。 既定値は、SelectionMode.One です。

例外

代入された値が、SelectionMode 値ではありません。

次のコード例では、 メソッドを GetSelected 使用して、 内のどの ListBox 項目が選択されているかを判断して、選択されていない項目を選択し、選択されている項目の選択を解除する方法を示します。 また、この例では、 プロパティを SelectionMode 使用して を有効 ListBox にして複数の項目を選択し、 プロパティを Sorted 使用して、 内の ListBox 項目を自動的に並べ替える方法を示します。 この例では、 という名前listBox1ListBoxがフォームに追加され、この例で定義されているメソッドがフォームInitializeMyListBoxのイベントからLoad呼び出されている必要があります。

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

注釈

SelectionModeプロパティを使用すると、ユーザーが一度に選択できる項目のListBox数と、ユーザーが複数選択を行う方法を決定できます。 プロパティが SelectionModeSelectionMode.MultiExtended設定されている場合、Shift キーを押しながらマウスをクリックするか、Shift キーを押し、いずれかの方向キー (上方向キー、下方向キー、左方向キー、右方向キー) を押すと、以前に選択した項目から現在のアイテムに選択範囲が拡張されます。 Ctrl キーを押しながらマウスをクリックすると、リスト内の項目が選択または選択解除されます。 プロパティが に SelectionMode.MultiSimple設定されている場合、マウスをクリックするか SPACE キーを押すと、リスト内の項目が選択または選択解除されます。

適用対象

こちらもご覧ください