ListBox.GetSelected(Int32) Yöntem

Tanım

Belirtilen öğenin seçili olup olmadığını gösteren bir değer döndürür.

public:
 bool GetSelected(int index);
public bool GetSelected (int index);
member this.GetSelected : int -> bool
Public Function GetSelected (index As Integer) As Boolean

Parametreler

index
Int32

Öğenin seçilip seçilmediğini belirleyen sıfır tabanlı dizini.

Döndürülenler

true belirtilen öğe içinde seçiliyse ListBox; seçilmemişse, false.

Özel durumlar

index parametresi sıfırdan küçük veya sınıfın özelliğinin CountListBox.ObjectCollection değerine eşit veya ondan büyük.

Örnekler

Aşağıdaki kod örneği, seçilmeyen öğeleri seçmek ve seçilen öğelerin seçimini kaldırmak için içindeki ListBox hangi öğelerin seçildiğini belirlemek için yönteminin nasıl kullanılacağını GetSelected gösterir. Bu örnekte ayrıca, bir öğesinin birden fazla seçili öğeye sahip olmasını sağlamak ListBox için özelliğinin kullanılması SelectionMode gösterilmektedir ve özelliği, içindeki ListBox öğelerin otomatik olarak nasıl sıralanacağını göstermek için kullanılırSorted. Bu örnek, adlı bir ListBoxöğesinin bir forma eklenmesini ve InitializeMyListBox örnekte tanımlanan yöntemin formun olayından Load çağrılmış olmasını listBox1gerektirir.

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

Açıklamalar

Belirtilen öğenin seçili olup olmadığını hızla belirlemek için bu yöntemi kullanabilirsiniz. Bu yöntem, çoklu seçimdeki ListBox belirli bir öğe seçildiğinde belirli bir işlemin gerçekleştirilmesi gerektiğinde kullanışlıdır.

Şunlara uygulanır