ListBox.BeginUpdate メソッド

定義

項目を ListBox に 1 つずつ追加するときにパフォーマンスを維持するには、EndUpdate() メソッドが呼び出されるまでコントロールを再描画しないようにします。

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

次のコード例では、 メソッドと EndUpdate メソッドをBeginUpdate使用しながら、 に 5,000 個の項目をListBox追加します。 この例では、 という名前listBox1ListBoxコントロールが に追加され、このメソッドがフォーム内に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推奨される方法は、 クラスの ListBox.ObjectCollection メソッドを AddRange (の プロパティListBoxItems使用して) 使用することです。 これにより、1 回の操作で項目の配列をリストに追加できます。 ただし、 クラスの ListBox.ObjectCollection メソッドを使用してAdd一度に 1 つずつ項目を追加する場合は、 メソッドをBeginUpdate使用して、アイテムがリストに追加されるたびにコントロールが再描画ListBoxされないようにすることができます。 リストに項目を追加するタスクが完了したら、 メソッドを EndUpdate 呼び出して、 ListBox を再描画できるようにします。 この方法で項目を追加すると、多数の ListBox 項目がリストに追加されるときに のちらつきが発生するのを防ぐことができます。

適用対象

こちらもご覧ください