ListBox.SelectionMode ListBox.SelectionMode ListBox.SelectionMode ListBox.SelectionMode Property

定義

ListBox で項目を選択する方法を取得または設定します。Gets or sets the method in which items are selected in the 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 つ。One of the SelectionMode values. 既定値は、SelectionMode.One です。The default is SelectionMode.One.

例外

代入された値が、SelectionMode 値ではありません。The assigned value is not one of the SelectionMode values.

次のコード例は、選択され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して、が複数の項目を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

注釈

プロパティを使用すると、ユーザーが一度に選択ListBoxできる項目の数と、ユーザーが複数選択を行う方法を決定できます。 SelectionModeThe SelectionMode property enables you to determine how many items in the ListBox a user can select at one time and how the user can make multiple-selections. プロパティがにSelectionMode.MultiExtended設定されている場合は、shift キーを押しながらマウスを押すか、shift キーを押しながら方向キー (↑、↓、←、→) のいずれかをクリックすると、選択範囲が前に選択した項目から現在の項目に拡張されます。 SelectionMode.When the SelectionMode property is set to SelectionMode.MultiExtended, pressing SHIFT and clicking the mouse or pressing SHIFT and one of the arrow keys (UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW) extends the selection from the previously selected item to the current item. CTRL キーを押しながらマウスをクリックすると、一覧の項目が選択または選択解除されます。Pressing CTRL and clicking the mouse selects or deselects an item in the list. プロパティがにSelectionMode.MultiSimple設定されている場合は、マウスクリックまたは space キーを押すと、一覧の項目が選択または選択解除されます。When the property is set to SelectionMode.MultiSimple, a mouse click or pressing the SPACEBAR selects or deselects an item in the list.

適用対象

こちらもご覧ください