ListBox.BeginUpdate 方法

定義

藉由在呼叫 ListBox 方法之前防止繪製控制項,便可在每次將項目加入至 EndUpdate() 的同時維持效能。

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

範例

下列程式碼範例會在將五千個專案新增至 ListBox 時使用 BeginUpdateEndUpdate 方法。 這個範例要求 ListBox 已將名為 listBox1 的控制項新增至 , Form 而且這個方法會放在表單中,並從中呼叫。

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 的慣用方式是透過) ItemsListBox.ObjectCollection 屬性,使用 AddRange 類別的 ListBox 方法 (。 這可讓您在單一作業中將專案陣列新增至清單。 不過,如果您想要使用 Add 類別的 ListBox.ObjectCollection 方法一次新增一個專案,您可以使用 BeginUpdate 方法防止控制項在每次將專案新增至清單中時重繪 ListBox 。 完成將專案新增至清單的工作之後,請呼叫 EndUpdate 方法以啟用 ListBox 重新繪製。 當大量專案新增至清單時,新增專案的方式可能會防止閃爍的繪圖 ListBox

適用於

另請參閱