Share via


ListBox.SelectionMode Properti

Definisi

Mendapatkan atau mengatur metode di mana item dipilih di 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

Nilai Properti

Salah SelectionMode satu nilai. Default adalah SelectionMode.One.

Pengecualian

Nilai yang ditetapkan bukan salah SelectionMode satu nilai.

Contoh

Contoh kode berikut menunjukkan cara menggunakan GetSelected metode untuk menentukan item mana dalam ListBox yang dipilih untuk memilih item yang tidak dipilih dan membatalkan pilihan item yang dipilih. Contoh ini juga menunjukkan penggunaan SelectionMode properti untuk memungkinkan memiliki ListBox lebih dari satu item yang dipilih dan menggunakan Sorted properti untuk menunjukkan cara mengurutkan item secara ListBox otomatis. Contoh ini mengharuskan , ListBoxbernama listBox1, telah ditambahkan ke formulir dan bahwa metode yang InitializeMyListBox ditentukan dalam contoh dipanggil dari Load peristiwa formulir.

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

Keterangan

Properti SelectionMode memungkinkan Anda menentukan berapa banyak item dalam ListBox yang dapat dipilih pengguna pada satu waktu dan bagaimana pengguna dapat membuat beberapa pilihan. SelectionMode Saat properti diatur ke SelectionMode.MultiExtended, menekan SHIFT dan mengklik mouse atau menekan SHIFT dan salah satu tombol panah (PANAH ATAS, PANAH BAWAH, PANAH KIRI, dan PANAH KANAN) memperluas pilihan dari item yang dipilih sebelumnya ke item saat ini. Menekan CTRL dan mengklik mouse memilih atau membatalkan pilihan item dalam daftar. Saat properti diatur ke SelectionMode.MultiSimple, klik mouse atau tekan BILAH SPASI memilih atau membatalkan pilihan item dalam daftar.

Berlaku untuk

Lihat juga