Freigeben über


ListBox-Klasse

Stellt ein Windows-Steuerelement zum Anzeigen einer Liste von Elementen dar.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class ListBox
    Inherits ListControl
'Usage
Dim instance As ListBox
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class ListBox : ListControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class ListBox : public ListControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class ListBox extends ListControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class ListBox extends ListControl

Hinweise

Das ListBox-Steuerelement ermöglicht dem Benutzer das Anzeigen einer Liste von Elementen, die durch Klicken ausgewählt werden können. Ein ListBox-Steuerelement ermöglicht unter Verwendung der SelectionMode-Eigenschaft die einfache oder mehrfache Auswahl. ListBox stellt außerdem die MultiColumn-Eigenschaft bereit, um die Anzeige von Elementen in Spalten statt in einer einfachen vertikalen Liste zu ermöglichen. Damit kann das Steuerelement eine größere Anzahl sichtbarer Elemente anzeigen, und der Benutzer muss keinen Bildlauf zu einem Element mehr durchführen.

In der Regel wird die Aufgabe des Zeichnens der in ListBox anzuzeigenden Elemente von Windows behandelt. Mithilfe der DrawMode-Eigenschaft können Sie das MeasureItem-Ereignis und das DrawItem-Ereignis behandeln, um das automatische Zeichnen von Windows zu überschreiben und die Elemente selbst zu zeichnen. Mit Ownerdrawn-ListBox-Steuerelementen können Sie Elemente oder Bilder mit unterschiedlicher Höhe oder den Text für die einzelnen Elemente in der Liste in einer anderen Farbe oder Schriftart anzeigen lassen. Die HorizontalExtent-Eigenschaft sowie GetItemHeight und GetItemRectangle bieten weitere Unterstützung beim Zeichnen von eigenen Elementen.

Neben den Funktionen zum Anzeigen und Auswählen bietet die ListBox auch Features, mit denen Sie der ListBox einfach Elemente hinzufügen und Text in den Listenelementen suchen können. Mithilfe der BeginUpdate-Methode und der EndUpdate-Methode können Sie der ListBox eine große Anzahl von Elementen hinzufügen, ohne dass das Steuerelement bei jedem Hinzufügen eines Elements zur Liste neu aufgebaut wird. Mithilfe der FindString-Methode und der FindStringExact-Methode können Sie nach einem Element in der Liste suchen, das eine bestimmte Suchzeichenfolge enthält.

Die Eigenschaften Items, SelectedItems und SelectedIndices bieten Zugriff auf die drei Auflistungen, die von ListBox verwendet werden. In der folgenden Tabelle werden die drei von ListBox verwendeten Auflistungen sowie ihre Verwendung im Steuerelement erläutert.

Auflistungsklasse

Verwenden Sie diese Klasse innerhalb der ListBox

ListBox.ObjectCollection

Enthält sämtliche im ListBox-Steuerelement enthaltenen Elemente.

ListBox.SelectedObjectCollection

Enthält eine Auflistung der ausgewählten Elemente, wobei es sich um eine Teilmenge der im ListBox-Steuerelement enthaltenen Elemente handelt.

ListBox.SelectedIndexCollection

Enthält eine Auflistung der ausgewählten Indizes, wobei es sich um eine Teilmenge der Indizes von ListBox.ObjectCollection handelt. Diese Indizes geben die ausgewählten Elemente an.

In den folgenden drei Beispielen werden die drei indizierten Auflistungen gezeigt, die von der ListBox-Klasse unterstützt werden.

In der folgenden Tabelle wird anhand eines Beispiels gezeigt, wie die ListBox.ObjectCollection die Elemente von ListBox sowie deren Auswahlzustand in einer Beispiel-ListBox speichert.

Index

Element

Auswahlzustand in ListBox

0

Objekt1

Nicht ausgewählt

1

Objekt2

Ausgewählt

2

Objekt3

Nicht ausgewählt

3

Objekt4

Ausgewählt

4

Objekt5

Ausgewählt

Auf der Grundlage der in der vorherigen Tabelle angezeigten ListBox.ObjectCollection zeigt diese Tabelle, wie die ListBox.SelectedObjectCollection angezeigt würde.

Index

Element

0

Objekt2

1

Objekt4

2

Objekt5

Auf der Grundlage der in der vorherigen Tabelle angezeigten ListBox.ObjectCollection zeigt diese Tabelle, wie die ListBox.SelectedIndexCollection angezeigt würde.

Index

Index von Element

0

1

1

3

2

4

Mit der Add-Methode der ListBox.ObjectCollection-Klasse können der ListBox Elemente hinzugefügt werden. Die Add-Methode kann jedes Objekt akzeptieren, wenn der ListBox ein Member hinzugefügt wird. Wenn der ListBox ein Objekt hinzugefügt wird, verwendet das Steuerelement den in der ToString-Methode des Objekts definierten Text, es sei denn, im Objekt ist ein Membername in der DisplayMember-Eigenschaft angegeben. Sie können Elemente nicht nur mithilfe der Add-Methode der ListBox.ObjectCollection-Klasse, sondern auch über die DataSource-Eigenschaft der ListControl-Klasse hinzufügen.

Hinweis

Wenn eine ListBox, ComboBox oder eine CheckedListBox in einem Windows-Basisformular vorhanden ist und Sie die Zeichenfolgenauflistungen dieser Steuerelemente in einem abgeleiteten Windows-Formular ändern möchten, müssen die Zeichenfolgenauflistungen dieser Steuerelemente in dem Windows-Basisformular leer sein. Wenn die Zeichenfolgenauflistungen nicht leer sind, werden sie durch Ableiten eines weiteren Windows-Formulars schreibgeschützt.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie ein ListBox-Steuerelement erstellt wird, das mehrere Elemente in Spalten anzeigt und in dessen Liste mehrere Elemente ausgewählt werden können. Im Code für das Beispiel werden der ListBox 50 Elemente mithilfe der Add-Methode der ListBox.ObjectCollection-Klasse hinzugefügt. Anschließend werden drei Elemente aus der Liste mithilfe der SetSelected-Methode ausgewählt. Im Code werden dann mithilfe der SelectedItems-Eigenschaft Werte aus der ListBox.SelectedObjectCollection-Auflistung sowie mithilfe der SelectedIndices-Eigenschaft Werte aus ListBox.SelectedIndexCollection angezeigt. In diesem Beispiel muss sich der Code in einem Form befinden und aus diesem aufgerufen werden.

Private Sub button1_Click(sender As Object, e As System.EventArgs)
    ' Create an instance of the ListBox.
    Dim listBox1 As New ListBox()
    ' Set the size and location of the ListBox.
    listBox1.Size = New System.Drawing.Size(200, 100)
    listBox1.Location = New System.Drawing.Point(10, 10)
    ' Add the ListBox to the form.
    Me.Controls.Add(listBox1)
    ' Set the ListBox to display items in multiple columns.
    listBox1.MultiColumn = True
    ' Set the selection mode to multiple and extended.
    listBox1.SelectionMode = SelectionMode.MultiExtended
    
    ' Shutdown the painting of the ListBox as items are added.
    listBox1.BeginUpdate()
    ' Loop through and add 50 items to the ListBox.
    Dim x As Integer
    For x = 1 To 50
        listBox1.Items.Add("Item " & x.ToString())
    Next x
    ' Allow the ListBox to repaint and display the new items.
    listBox1.EndUpdate()
    
    ' Select three items from the ListBox.
    listBox1.SetSelected(1, True)
    listBox1.SetSelected(3, True)
    listBox1.SetSelected(5, True)
       
    ' Display the second selected item in the ListBox to the console.
    System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems(1).ToString())
    ' Display the index of the first selected item in the ListBox.
    System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices(0).ToString())
End Sub
private void button1_Click(object sender, System.EventArgs e)
{
   // Create an instance of the ListBox.
   ListBox listBox1 = new ListBox();
   // Set the size and location of the ListBox.
   listBox1.Size = new System.Drawing.Size(200, 100);
   listBox1.Location = new System.Drawing.Point(10,10);
   // Add the ListBox to the form.
   this.Controls.Add(listBox1);
   // Set the ListBox to display items in multiple columns.
   listBox1.MultiColumn = true;
   // Set the selection mode to multiple and extended.
   listBox1.SelectionMode = SelectionMode.MultiExtended;
 
   // Shutdown the painting of the ListBox as items are added.
   listBox1.BeginUpdate();
   // Loop through and add 50 items to the ListBox.
   for (int x = 1; x <= 50; x++)
   {
      listBox1.Items.Add("Item " + x.ToString());
   }
   // Allow the ListBox to repaint and display the new items.
   listBox1.EndUpdate();
      
   // Select three items from the ListBox.
   listBox1.SetSelected(1, true);
   listBox1.SetSelected(3, true);
   listBox1.SetSelected(5, true);

   // Display the second selected item in the ListBox to the console.
   System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
   // Display the index of the first selected item in the ListBox.
   System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());             
}
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   
   // Create an instance of the ListBox.
   ListBox^ listBox1 = gcnew ListBox;
   
   // Set the size and location of the ListBox.
   listBox1->Size = System::Drawing::Size( 200, 100 );
   listBox1->Location = System::Drawing::Point( 10, 10 );
   
   // Add the ListBox to the form.
   this->Controls->Add( listBox1 );
   
   // Set the ListBox to display items in multiple columns.
   listBox1->MultiColumn = true;
   
   // Set the selection mode to multiple and extended.
   listBox1->SelectionMode = SelectionMode::MultiExtended;
   
   // Shutdown the painting of the ListBox as items are added.
   listBox1->BeginUpdate();
   
   // Loop through and add 50 items to the ListBox.
   for ( int x = 1; x <= 50; x++ )
   {
      listBox1->Items->Add( String::Format( "Item {0}", x ) );

   }
   listBox1->EndUpdate();
   
   // Select three items from the ListBox.
   listBox1->SetSelected( 1, true );
   listBox1->SetSelected( 3, true );
   listBox1->SetSelected( 5, true );
   
   // Display the second selected item in the ListBox to the console.
   System::Diagnostics::Debug::WriteLine( listBox1->SelectedItems[ 1 ] );
   
   // Display the index of the first selected item in the ListBox.
   System::Diagnostics::Debug::WriteLine( listBox1->SelectedIndices[ 0 ] );
}
private void button1_Click(Object sender, System.EventArgs e)
{
    // Create an instance of the ListBox.
    ListBox listBox1 = new ListBox();

    // Set the size and location of the ListBox.
    listBox1.set_Size(new System.Drawing.Size(200,100));
    listBox1.set_Location(new System.Drawing.Point(10,10));

    // Add the ListBox to the form.
    this.get_Controls().Add(listBox1);

    // Set the ListBox to display items in multiple columns.
    listBox1.set_MultiColumn(true);

    // Set the selection mode to multiple and extended.
    listBox1.set_SelectionMode(SelectionMode.MultiExtended);

    // Shutdown the painting of the ListBox as items are added.
    listBox1.BeginUpdate();

    // Loop through and add 50 items to the ListBox.
    for (int x = 1; x <= 50; x++) {
        listBox1.get_Items().Add(("Item" + (new Integer(x)).ToString()));
    }

    // Allow the ListBox to repaint and display the new items.
    listBox1.EndUpdate();

    // Select three items from the ListBox.
    listBox1.SetSelected(1,true);
    listBox1.SetSelected(3,true);
    listBox1.SetSelected(5,true);

    // Display the second selected item in the ListBox to the console.
    System.Diagnostics.Debug.WriteLine
        (listBox1.get_SelectedItems().get_Item(1).ToString());

    // Display the index of the first selected item in the ListBox.
    System.Diagnostics.Debug.WriteLine((new Integer
        (listBox1.get_SelectedIndices().get_Item(0))).ToString());
} //button1_Click
private function button1_Click(sender : Object, e : System.EventArgs)
{
   // Create an instance of the ListBox.
   var listBox1 : ListBox = new ListBox();
   // Set the size and location of the ListBox.
   listBox1.Size = new System.Drawing.Size(200, 100);
   listBox1.Location = new System.Drawing.Point(10,10);
   // Add the ListBox to the form.
   this.Controls.Add(listBox1);
   // Set the ListBox to display items in multiple columns.
   listBox1.MultiColumn = true;
   // Set the selection mode to multiple and extended.
   listBox1.SelectionMode = SelectionMode.MultiExtended;
 
   // Shutdown the painting of the ListBox as items are added.
   listBox1.BeginUpdate();
   // Loop through and add 50 items to the ListBox.
   for (var x : int = 1; x <= 50; x++)
   {
      listBox1.Items.Add("Item " + x.ToString());
   }
   // Allow the ListBox to repaint and display the new items.
   listBox1.EndUpdate();
      
   // Select three items from the ListBox.
   listBox1.SetSelected(1, true);
   listBox1.SetSelected(3, true);
   listBox1.SetSelected(5, true);

   // Display the second selected item in the ListBox to the console.
   System.Diagnostics.Debug.WriteLine(listBox1.SelectedItems[1].ToString());
   // Display the index of the first selected item in the ListBox.
   System.Diagnostics.Debug.WriteLine(listBox1.SelectedIndices[0].ToString());             
}

Vererbungshierarchie

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ListControl
          System.Windows.Forms.ListBox
             System.Windows.Forms.CheckedListBox

Threadsicherheit

Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ListBox-Member
System.Windows.Forms-Namespace