Festlegen des Elements am Anfang einer Liste und des Elements, das den Fokus in der Liste hat

The following example identifies the top item displayed in a ListBox and the item that has the focus within the ListBox. This example uses the TopIndex property to identify the item displayed at the top of the ListBox and the ListIndex property to identify the item that has the focus. The user selects an item in the ListBox. Die angezeigten Werte von TopIndex und ListIndex werden aktualisiert, wenn der Benutzer ein Element auswählt oder wenn der Benutzer auf das CommandButton-Steuerelement klickt.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains:

  • Ein Bezeichnungsfeld-Steuerelement ( Label ) mit der Bezeichnung "Label1".

  • Ein Textfeld-Steuerelement ( TextBox ) mit der Bezeichnung "TextBox1".

  • Ein Bezeichnungsfeld-Steuerelement ( Label ) mit der Bezeichnung "Label2".

  • Ein Textfeld-Steuerelement ( TextBox ) mit der Bezeichnung "TextBox2".

  • Eine Befehlsschaltfläche ( CommandButton ) mit der Bezeichnung "CommandButton1".

  • A ListBox named ListBox1 that is bound to the Subject field.

Sub CommandButton1_Click() 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox2") 
 
 ListBox1.TopIndex = ListBox1.ListIndex 
 TextBox1.Text = ListBox1.TopIndex 
 TextBox2.Text = ListBox1.ListIndex 
End Sub 
 
Sub Item_PropertyChange(byval pname) 
 if pname = "Subject" then 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox2") 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 
 TextBox1.Text = ListBox1.TopIndex 
 TextBox2.Text = ListBox1.ListIndex 
 end if 
End Sub 
 
Sub Item_Open() 
 Set ListBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ListBox1") 
 Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("CommandButton1") 
 Set TextBox1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox1") 
 Set TextBox2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("TextBox2") 
 Set Label1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label1") 
 Set Label2 = Item.GetInspector.ModifiedFormPages("P.2").Controls("Label2") 
 
 For i = 0 To 24 
 ListBox1.AddItem "Choice " & (i + 1) 
 Next 
 ListBox1.Height = 66 
 CommandButton1.Caption = "Move to top of list" 
 CommandButton1.AutoSize = True 
 CommandButton1.TakeFocusOnClick = False 
 
 Label1.Caption = "Index of top item" 
 TextBox1.Text = ListBox1.TopIndex 
 
 Label2.Caption = "Index of current item" 
 Label2.AutoSize = True 
 Label2.WordWrap = False 
 TextBox2.Text = ListBox1.ListIndex 
End Sub

Support und Feedback

Haben Sie Fragen oder Feedback zu Office VBA oder zu dieser Dokumentation? Unter Office VBA-Support und Feedback finden Sie Hilfestellung zu den Möglichkeiten, wie Sie Support erhalten und Feedback abgeben können.