Share via


ListBox.SelectionMode Vlastnost

Definice

Získá nebo nastaví metodu, ve které jsou vybrány položky v 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

Hodnota vlastnosti

Jedna z SelectionMode hodnot. Výchozí formát je SelectionMode.One.

Výjimky

Přiřazená hodnota není jednou z SelectionMode hodnot.

Příklady

Následující příklad kódu ukazuje, jak pomocí GetSelected metody určit, které položky v objektu ListBox jsou vybrány, aby bylo možné vybrat položky, které nejsou vybrány, a zrušit výběr vybraných položek. Příklad také ukazuje použití SelectionMode vlastnosti k povolení více než jednu vybranou ListBox položku a používá Sorted vlastnost k předvedení, jak automaticky seřadit položky.ListBox Tento příklad vyžaduje, aby ListBoxbyla do formuláře přidána metoda s názvem listBox1, a aby InitializeMyListBox metoda definovaná v příkladu byla volána z Load události formuláře.

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

Poznámky

Vlastnost SelectionMode umožňuje určit, kolik položek může ListBox uživatel vybrat najednou a jak může provést vícenásobný výběr. SelectionMode Když je vlastnost nastavená na SelectionMode.MultiExtended, stisknutím klávesy SHIFT a kliknutím myši nebo klávesou SHIFT a jednou ze šipkových kláves (ŠIPKA NAHORU, ŠIPKA DOLŮ, ŠIPKA VLEVO a ŠIPKA VPRAVO) rozšíříte výběr z dříve vybrané položky na aktuální položku. Stisknutím klávesy CTRL a kliknutím myši vyberete nebo zrušíte výběr položky v seznamu. Když je vlastnost nastavená na SelectionMode.MultiSimple, kliknutím myši nebo stisknutím mezerníku vyberete nebo zrušíte výběr položky v seznamu.

Platí pro

Viz také