共用方式為


ListBox.BeginUpdate 方法

定義

在項目逐一添加 ListBox 時,透過防止控制點繪製直到 EndUpdate() 方法被呼叫來維持效能。

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

範例

以下程式碼範例使用 和 EndUpdate 方法BeginUpdate,並在 中加入五千個項目。ListBox 此範例要求一個 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 的首選方式是使用 AddRange 類別的方法 ListBox.ObjectCollection (透過 ItemsListBox屬性)。 這讓你能在一次操作中新增一組項目。 不過,如果你想用 Add 類別的方法 ListBox.ObjectCollection 一個一個新增項目,可以用這個 BeginUpdate 方法防止每次新增項目時控制項重新繪製 ListBox 。 完成將項目加入清單的任務後,呼叫 EndUpdate 該方法來啟用 ListBox 重新繪製。 這種新增項目的方式可以避免在大量項目加入列表中時出現閃爍繪製 ListBox 的情況。

適用於

另請參閱