ComboBox.BeginUpdate 方法

定義

當項目一次一筆加入 ComboBox 時可維護效能。Maintains performance when items are added to the ComboBox one at a time.

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

範例

下列程式碼範例顯示 BeginUpdateEndUpdate 方法的用法。The following code example shows the usage of the BeginUpdate and EndUpdate methods. 此範例是 ComboBox 類別總覽中完整程式碼範例的一部分。The example is part of a complete code example in the ComboBox class overview.

void addGrandButton_Click( Object^ sender, System::EventArgs^ e )
{
   comboBox1->BeginUpdate();
   for ( int i = 0; i < 1000; i++ )
   {
      comboBox1->Items->Add( "New Item " + i.ToString() );
   }
   comboBox1->EndUpdate();
}
private void addGrandButton_Click(object sender, System.EventArgs e) {
    comboBox1.BeginUpdate();
    for (int i = 0; i < 1000; i++) {
        comboBox1.Items.Add("New Item " + i.ToString());
    }
    comboBox1.EndUpdate();
}
Private Sub addGrandButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    comboBox1.BeginUpdate()
    Dim I As Integer
    For I = 0 To 1000
        comboBox1.Items.Add("New Item " + i.ToString())
    Next
    comboBox1.EndUpdate()
End Sub

備註

這個方法會防止控制項繪製,直到呼叫 EndUpdate 方法為止。This method prevents the control from painting until the EndUpdate method is called.

將專案加入至 ComboBox 的慣用方法是使用 ComboBox.ObjectCollection 類別的 AddRange 方法(透過 ComboBoxItems 屬性)。The preferred way to add items to the ComboBox is to use the AddRange method of the ComboBox.ObjectCollection class (through the Items property of the ComboBox). 這可讓您一次將一個專案陣列加入清單中。This enables you to add an array of items to the list at one time. 不過,如果您想要使用 ComboBox.ObjectCollection 類別的 Add 方法一次加入一個專案,您可以使用 BeginUpdate 方法,避免每次將專案新增至清單時,控制項重新繪製 ComboBoxHowever, if you want to add items one at a time using the Add method of the ComboBox.ObjectCollection class, you can use the BeginUpdate method to prevent the control from repainting the ComboBox each time an item is added to the list. 完成將專案新增至清單的工作之後,請呼叫 EndUpdate 方法,讓 ComboBox 重新繪製。Once you have completed the task of adding items to the list, call the EndUpdate method to enable the ComboBox to repaint. 這種新增專案的方式可在將大量專案新增至清單時,防止 ComboBox 繪製期間發生閃爍。This way of adding items can prevent flicker during the drawing of the ComboBox when a large number of items are being added to the list.

適用於