ListBox.SelectionMode Propiedad

Definición

Obtiene o establece el método en el que se seleccionan los elementos del control 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

Valor de propiedad

Uno de los valores de SelectionMode. De manera predeterminada, es SelectionMode.One.

Excepciones

El valor asignado no es uno de los valores de SelectionMode.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar el GetSelected método para determinar qué elementos de un elemento ListBox se seleccionan para seleccionar los elementos que no están seleccionados y anular la selección de los elementos seleccionados. En el ejemplo también se muestra cómo usar la SelectionMode propiedad para permitir que un ListBox elemento tenga más de un elemento seleccionado y use la Sorted propiedad para mostrar cómo ordenar elementos de forma ListBox automática. En este ejemplo se requiere que se haya agregado un ListBox, denominado listBox1, a un formulario y que se llame al InitializeMyListBox método definido en el ejemplo desde el Load evento del formulario.

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

Comentarios

La SelectionMode propiedad permite determinar cuántos elementos de ListBox un usuario pueden seleccionar a la vez y cómo el usuario puede realizar varias selecciones. Cuando la SelectionMode propiedad se establece SelectionMode.MultiExtendeden , al presionar MAYÚS y hacer clic en el mouse o presionar MAYÚS y una de las teclas de dirección (FLECHA ARRIBA, FLECHA ABAJO, FLECHA IZQUIERDA y FLECHA DERECHA) extiende la selección del elemento seleccionado anteriormente al elemento actual. Al presionar CTRL y hacer clic en el mouse, se selecciona o anula la selección de un elemento de la lista. Cuando la propiedad se establece SelectionMode.MultiSimpleen , un mouse hace clic o presiona la barra ESPACIADORA selecciona o anula la selección de un elemento de la lista.

Se aplica a

Consulte también