ListBox.TopIndex Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger indexet för det första synliga objektet i 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
Egenskapsvärde
Det nollbaserade indexet för det första synliga objektet i kontrollen.
- Attribut
Exempel
Följande kodexempel visar hur du använder SelectedIndex egenskapen med TopIndex egenskapen för att flytta det markerade objektet överst i listan över objekt i visningsområdet i ListBox. Exemplet visar vidare hur du tar bort objekt med hjälp av metoden RemoveAt i klassen System.Windows.Forms.ListBox.ObjectCollection och hur du rensar alla objektmarkeringar med hjälp av metoden ClearSelected. Koden flyttar först det markerade objektet längst upp i ListBox listan. Koden tar sedan bort alla objekt före det markerade objektet och rensar alla markeringar i ListBox. Det här exemplet kräver att ett ListBox innehållande objekt läggs till i ett formulär och att ett objekt för närvarande är markerat i 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
Kommentarer
Till en början är objektet med indexpositionen noll (0) överst i den synliga regionen i ListBox. Om innehållet i ListBox har rullats kan ett annat objekt finnas överst i kontrollens visningsområde. Du kan använda den här egenskapen för att hämta indexet ListBox.ObjectCollection inom för ListBox objektet som för närvarande är placerat överst i kontrollens synliga region. Du kan också använda den här egenskapen för att placera ett objekt i listan överst i kontrollens synliga region.