Бөлісу құралы:


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))
         // 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 пользователю можно выбрать в один раз и как пользователь может выбрать несколько элементов. SelectionMode Если для свойства задано SelectionMode.MultiExtendedзначение , нажатие клавиш SHIFT и нажатие мыши или нажатие клавиш SHIFT и одна из клавиш со стрелками (СТРЕЛКА ВВЕРХ, СТРЕЛКА ВНИЗ, СТРЕЛКА ВЛЕВО и СТРЕЛКА ВПРАВО) расширяет выбор из ранее выбранного элемента к текущему элементу. Нажатие клавиш CTRL и нажатие мыши выбирает или отменяет выбор элемента в списке. Если для свойства задано SelectionMode.MultiSimpleзначение, щелчок мыши или нажатие клавиши ПРОБЕЛ выбирает или отменяет выбор элемента в списке.

Применяется к

См. также раздел