ListBox.ClearSelected Metode
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.
Batal memilih semua item di ListBox.
public:
void ClearSelected();
public void ClearSelected();
member this.ClearSelected : unit -> unit
Public Sub ClearSelected ()
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
Memanggil metode ini setara dengan mengatur SelectedIndex properti ke negatif (-1). Anda dapat menggunakan metode ini untuk dengan cepat membatalkan pilihan semua item dalam daftar.