Freigeben über


ListBox.BeginUpdate-Methode

Erhält die Leistung aufrecht, während der ListBox Elemente einzeln hinzugefügt werden, indem das Zeichnen des Steuerelements bis zum Aufruf der EndUpdate-Methode verhindert wird.

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

Syntax

'Declaration
Public Sub BeginUpdate
'Usage
Dim instance As ListBox

instance.BeginUpdate
public void BeginUpdate ()
public:
void BeginUpdate ()
public void BeginUpdate ()
public function BeginUpdate ()

Hinweise

Die empfohlene Vorgehensweise um der ListBox Elemente hinzuzufügen besteht im Verwenden der AddRange-Methode der ListBox.ObjectCollection-Klasse (über die Items-Eigenschaft von ListBox). So können Sie der Liste ein Array von Elementen in einem einzigen Vorgang hinzufügen. Wenn Sie allerdings Elemente einzeln mithilfe der Add-Methode der ListBox.ObjectCollection-Klasse hinzufügen möchten, können Sie mithilfe der BeginUpdate-Methode verhindern, dass ListBox jedes Mal neu aufgebaut wird, wenn der Liste ein neues Element hinzugefügt wird. Sobald der Vorgang des Hinzufügens von Elementen zur Liste abgeschlossen ist, rufen Sie die EndUpdate-Methode auf, damit die ListBox neu aufgebaut werden kann. Durch diese Vorgehensweise beim Hinzufügen von Elementen kann das Flackern beim Zeichnen der ListBox verhindert werden, das entsteht, wenn der Liste eine große Zahl von Elementen hinzugefügt wird.

Beispiel

Im folgenden Codebeispiel werden die BeginUpdate-Methode und die EndUpdate-Methode verwendet, während einer ListBox 5000 Elemente hinzugefügt werden. Für dieses Beispiel muss einem Form das ListBox-Steuerelement listBox1 hinzugefügt werden; außerdem muss diese Methode im Formular platziert und aus diesem aufgerufen werden.

Public Sub AddToMyListBox()
    ' Stop the ListBox from drawing while items are added.
    listBox1.BeginUpdate()
       
    ' Loop through and add five thousand new items.
    Dim x As Integer
    For x = 1 To 4999
        listBox1.Items.Add("Item " & x.ToString())
    Next x
    ' End the update process and force a repaint of the ListBox.
    listBox1.EndUpdate()
End Sub
public void AddToMyListBox()
{
   // Stop the ListBox from drawing while items are added.
   listBox1.BeginUpdate();

   // Loop through and add five thousand new items.
   for(int x = 1; x < 5000; x++)
   {
      listBox1.Items.Add("Item " + x.ToString());   
   }
   // End the update process and force a repaint of the ListBox.
   listBox1.EndUpdate();
}
void AddToMyListBox()
{
   // Stop the ListBox from drawing while items are added.
   listBox1->BeginUpdate();

   // Loop through and add five thousand new items.
   for ( int x = 1; x < 5000; x++ )
   {
      listBox1->Items->Add( String::Format( "Item {0}", x ) );
   }
   listBox1->EndUpdate();
}
public void AddToMyListBox()
{
    // Stop the ListBox from drawing while items are added.
    listBox1.BeginUpdate();

    // Loop through and add five thousand new items.
    for (int x = 1; x < 5000; x++) {
        listBox1.get_Items().Add(("Item" + (new Integer(x)).ToString()));
    }

    // End the update process and force a repaint of the ListBox.
    listBox1.EndUpdate();
} //AddToMyListBox
function AddToMyListBox(){
    // Stop the ListBox from drawing while items are added.
    listBox1.BeginUpdate()
       
    // Loop through and add five thousand new items.
    for(var x = 0; x < 5000; x++)
        listBox1.Items.Add("Item " + x.ToString())
    // End the update process and force a repaint of the ListBox.
    listBox1.EndUpdate()
}

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

Siehe auch

Referenz

ListBox-Klasse
ListBox-Member
System.Windows.Forms-Namespace
EndUpdate