ListBox.GetSelected(Int32) Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
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çili olup olmadığını belirleyen sıfır tabanlı dizini.
Döndürülenler
true belirtilen öğe şu anda içinde ListBoxseçiliyse; değilse, false.
Özel durumlar
index parametresi sıfırdan küçük veya sınıfın özelliğinin CountListBox.ObjectCollection değerine eşit veya sıfırdan 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 hangi öğelerin ListBox seçildiğini belirlemek için yönteminin nasıl kullanılacağını GetSelected gösterir. Örnekte ayrıca, bir öğesinin SelectionMode birden çok seçili öğeye sahip olmasını sağlamak ListBox için özelliğinin kullanılması gösterilmektedir ve otomatik olarak öğeleri ListBox sıralamayı göstermek için özelliğini kullanırSorted. Bu örnek, adlı bir ListBoxöğesinin 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))
// 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 bir öğenin seçili olup olmadığını hızla belirlemek için bu yöntemi kullanabilirsiniz. Bu yöntem, birden çok seçimdeki ListBox belirli bir öğe seçildiğinde belirli bir işlemin gerçekleştirilmesi gerektiğinde kullanışlıdır.