Nyelv

ListBox.SelectionMode Tulajdonság

Definíció

Lekéri vagy beállítja azt a metódust, amelyben az elemek ki vannak jelölve a 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

Tulajdonság értéke

Az egyik SelectionMode érték. Az alapértelmezett érték a SelectionMode.One.

Kivételek

A hozzárendelt érték nem tartozik az SelectionMode értékek közé.

Példák

Az alábbi példakód bemutatja, hogyan határozhatja meg a GetSelected metódussal, hogy mely elemek legyenek kijelölve a kijelölt elemek ListBox kiválasztásához és a kijelölt elemek kijelölésének megszüntetéséhez. A példa azt is bemutatja, hogy a SelectionMode tulajdonság használatával lehetővé teszi egynél ListBox több kijelölt elem használatát, és a Sorted tulajdonság használatával bemutatja, hogyan rendezheti automatikusan az ListBox elemeket. Ehhez a példához hozzá kell adni egy ListBoxelnevezett listBox1metódust egy űrlaphoz, és a InitializeMyListBox példában definiált metódust az Load űrlap eseményéből kell meghívni.

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

Megjegyzések

A SelectionMode tulajdonság lehetővé teszi annak meghatározását, hogy a ListBox felhasználó hány elemet jelölhet ki egyszerre, és hogy a felhasználó hogyan végezhet több kijelölést. Ha a tulajdonság be van állítvaSelectionMode, nyomja le a SelectionMode.MultiExtended SHIFT billentyűt, és kattintson az egérre, vagy nyomja le a SHIFT billentyűt, és az egyik nyílbillentyű (FEL, LE, BALRA nyíl és JOBBRA nyíl) kiterjeszti a kijelölést a korábban kijelölt elemről az aktuális elemre. A CTRL billentyűt lenyomva az egérre kattintva kijelöl vagy töröl egy elemet a listában. Ha a tulajdonság értéke be van állítva SelectionMode.MultiSimple, a SZÓKÖZ billentyűt lenyomva vagy kattintva kattintással kijelölhet vagy törölhet egy elemet a listában.

A következőre érvényes:

Lásd még