ListBox.BeginUpdate 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
通过阻止控件在调用方法之前EndUpdate(),一次将项添加到ListBox一个控件,从而保持性能。
public:
void BeginUpdate();
public void BeginUpdate();
member this.BeginUpdate : unit -> unit
Public Sub BeginUpdate ()
示例
下面的代码示例在向 a ListBox中添加 5000 个项时使用 BeginUpdate and EndUpdate 方法。 此示例要求已将名为
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 。