通过


ListBox.BeginUpdate 方法

定义

通过阻止控件在调用方法之前EndUpdate(),一次将项添加到ListBox一个控件,从而保持性能。

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

示例

下面的代码示例在向 a ListBox中添加 5000 个项时使用 BeginUpdate and EndUpdate 方法。 此示例要求已将名为 /> 的控件添加到 a,并且此方法放置在窗体中并从中调用。

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 (通过 Items 的属性 ListBox)。 这样,便可以在单个操作中将项数组添加到列表中。 但是,如果要使用 Add 类的方法 ListBox.ObjectCollection 一次添加一个项,则可以使用 BeginUpdate 该方法防止控件在每次将项添加到列表中时重新绘制 ListBox 。 完成将项添加到列表的任务后,调用 EndUpdate 该方法以启用 ListBox 重新修补。 这样一来添加项可以防止将大量项目添加到列表中时闪烁的绘图 ListBox

适用于

另请参阅