Export (0) Print
Expand All

ListBox.EndUpdate Method

Resumes painting the ListBox control after painting is suspended by the BeginUpdate method.

[Visual Basic]
Public Sub EndUpdate()
[C#]
public void EndUpdate();
[C++]
public: void EndUpdate();
[JScript]
public function EndUpdate();

Remarks

The preferred way to add items to the ListBox is to use the AddRange method of the ListBox.ObjectCollection class (through the Items property of the ListBox). This enables you to add an array of items to the list at one time. However, if you want to add items one at a time using the Add method of the ListBox.ObjectCollection class, you can use the BeginUpdate method to prevent the control from repainting the ListBox each time an item is added to the list. Once you have completed the task of adding items to the list, call the EndUpdate method to enable the ListBox to repaint. This way of adding items can prevent flickered drawing of the ListBox when a large number of items are being added to the list.

Example

The following example uses the BeginUpdate and EndUpdate methods while adding five thousand items to a ListBox. This example assumes that a ListBox control, named listBox1, has been added to a Form and that this method is placed within the form and called from it.

[Visual Basic] 
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


[C#] 
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();
}


[C++] 
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(String::Format( S"Item {0}", __box(x)));   
   }
   // End the update process and force a repaint of the ListBox.
   listBox1->EndUpdate();
}


[JScript] 
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()
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ListBox Class | ListBox Members | System.Windows.Forms Namespace | BeginUpdate

Show:
© 2015 Microsoft