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 값 중 하나입니다. 기본값은 SelectionMode.One입니다.

예외

할당된 값이 SelectionMode 값 중 하나가 아닌 경우

예제

다음 코드 예제를 사용 GetSelected 하는 방법에 설명 합니다 선택 되지 않은 항목을 선택 하 고 선택 된 항목을 ListBox 선택 취소 하기 위해 에서 항목을 확인 하는 방법입니다. 예제도 사용 하 여 보여 줍니다는 SelectionMode 속성을 사용를 ListBox 개를 사용 하 여 항목을 선택 합니다 Sorted 속성에서 항목을 정렬 하는 방법을 보여 줍니다는 ListBox 자동으로. 이 예제에서는 ListBox라는 listBox1가 양식에 추가되고 예제에 정의된 메서드가 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설정되면 스페이스바를 마우스로 클릭하거나 눌러 목록에서 항목을 선택하거나 선택 취소합니다.

적용 대상

추가 정보