ListBox.SelectedIndex Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur indeks berbasis nol dari item yang saat ini dipilih dalam ListBox.
public:
virtual property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public override int SelectedIndex { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer
Nilai Properti
Indeks berbasis nol dari item yang saat ini dipilih. Nilai negatif satu (-1) dikembalikan jika tidak ada item yang dipilih.
- Atribut
Pengecualian
Nilai yang ditetapkan kurang dari -1 atau lebih besar dari atau sama dengan jumlah item.
Properti SelectionMode disetel ke None.
Contoh
Contoh kode berikut menunjukkan cara menggunakan SelectedIndex properti dengan TopIndex properti untuk memindahkan item yang saat ini dipilih ke bagian atas daftar item di area ListBoxtampilan . Contoh selanjutnya menunjukkan cara menghapus item menggunakan RemoveAt metode System.Windows.Forms.ListBox.ObjectCollection kelas, dan cara menghapus semua pilihan item menggunakan ClearSelected metode . Kode pertama-tama memindahkan item yang saat ini dipilih di ListBox bagian atas daftar. Kode kemudian menghapus semua item sebelum item yang saat ini dipilih dan menghapus semua pilihan di ListBox. Contoh ini mengharuskan item yang ListBox berisi ditambahkan ke formulir dan item saat ini dipilih di ListBox.
private:
void RemoveTopItems()
{
// Determine if the currently selected item in the ListBox
// is the item displayed at the top in the ListBox.
if ( listBox1->TopIndex != listBox1->SelectedIndex )
// Make the currently selected item the top item in the ListBox.
listBox1->TopIndex = listBox1->SelectedIndex;
// Remove all items before the top item in the ListBox.
for ( int x = (listBox1->SelectedIndex - 1); x >= 0; x-- )
{
listBox1->Items->RemoveAt( x );
}
// Clear all selections in the ListBox.
listBox1->ClearSelected();
}
private void RemoveTopItems()
{
// Determine if the currently selected item in the ListBox
// is the item displayed at the top in the ListBox.
if (listBox1.TopIndex != listBox1.SelectedIndex)
// Make the currently selected item the top item in the ListBox.
listBox1.TopIndex = listBox1.SelectedIndex;
// Remove all items before the top item in the ListBox.
for (int x = (listBox1.SelectedIndex -1); x >= 0; x--)
{
listBox1.Items.RemoveAt(x);
}
// Clear all selections in the ListBox.
listBox1.ClearSelected();
}
Private Sub RemoveTopItems()
' Determine if the currently selected item in the ListBox
' is the item displayed at the top in the ListBox.
If listBox1.TopIndex <> listBox1.SelectedIndex Then
' Make the currently selected item the top item in the ListBox.
listBox1.TopIndex = listBox1.SelectedIndex
End If
' Remove all items before the top item in the ListBox.
Dim x As Integer
For x = listBox1.SelectedIndex - 1 To 0 Step -1
listBox1.Items.RemoveAt(x)
Next x
' Clear all selections in the ListBox.
listBox1.ClearSelected()
End Sub
Keterangan
Untuk standar ListBox, Anda dapat menggunakan properti ini untuk menentukan indeks item yang dipilih di ListBox.
SelectionMode Jika properti diatur ListBox ke atau SelectionMode.MultiSimpleSelectionMode.MultiExtended (yang menunjukkan beberapa pilihan ListBox) dan beberapa item dipilih dalam daftar, properti ini dapat mengembalikan indeks ke item yang dipilih.
Untuk mengambil koleksi yang berisi indeks semua item yang dipilih dalam beberapa pilihan ListBox, gunakan SelectedIndices properti . Jika Anda ingin mendapatkan item yang saat ini dipilih di ListBox, gunakan SelectedItem properti . Selain itu, Anda dapat menggunakan SelectedItems properti untuk mendapatkan semua item yang dipilih dalam beberapa pilihan ListBox.