ListBox.GetSelected(Int32) Yöntem

Tanım

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

C#
public bool GetSelected(int index);

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.

C#
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;
}

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

Ürün Sürümler
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10