ListBox.EndUpdate 方法

定义

BeginUpdate() 方法挂起绘制后,该方法恢复绘制 ListBox 控件。Resumes painting the ListBox control after painting is suspended by the BeginUpdate() method.

public:
 void EndUpdate();
public void EndUpdate ();
member this.EndUpdate : unit -> unit
Public Sub EndUpdate ()

示例

下面的代码示例使用 BeginUpdateEndUpdate 方法,同时将5000项添加到 ListBoxThe following code example uses the BeginUpdate and EndUpdate methods while adding five thousand items to a ListBox. 此示例要求已将名为 listBox1ListBox 控件添加到 Form,并将此方法放置在窗体中并从其调用。This example requires 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.

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.Items.Add("Item " + x.ToString());   
   }
   // End the update process and force a repaint of the ListBox.
   listBox1.EndUpdate();
}

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

注解

ListBox 中添加项的首选方法是使用 ListBox.ObjectCollection 类的 AddRange 方法(通过 ListBoxItems 属性)。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. 但是,如果你想要使用 ListBox.ObjectCollection 类的 Add 方法一次添加一个项,则可以使用 BeginUpdate 方法来防止控件在每次向列表中添加项时重新绘制 ListBoxHowever, 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. 完成向列表中添加项的任务后,调用 EndUpdate 方法,以允许 ListBox 重绘。Once you have completed the task of adding items to the list, call the EndUpdate method to enable the ListBox to repaint. 当向列表中添加大量项时,这种添加项的方法可能会阻止 flickered 的 ListBox 绘图。This way of adding items can prevent flickered drawing of the ListBox when a large number of items are being added to the list.

适用于

另请参阅