ListBox.TopIndex 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 item pertama yang terlihat di ListBox.
public:
property int TopIndex { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int TopIndex { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.TopIndex : int with get, set
Public Property TopIndex As Integer
Nilai Properti
Indeks berbasis nol dari item pertama yang terlihat dalam kontrol.
- Atribut
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
Awalnya, item dengan posisi indeks nol (0) berada di bagian atas wilayah yang terlihat dari ListBox. Jika konten ListBox telah digulir, item lain mungkin berada di bagian atas area tampilan kontrol. Anda dapat menggunakan properti ini untuk mendapatkan indeks dalam ListBox.ObjectCollection untuk ListBox item yang saat ini diposisikan di bagian atas wilayah kontrol yang terlihat. Anda juga dapat menggunakan properti ini untuk memosisikan item dalam daftar di bagian atas wilayah kontrol yang terlihat.