.NET Framework 類別庫
ListBox..::.BeginUpdate 方法

更新:2007 年 11 月

藉由在呼叫 EndUpdate 方法之前防止繪製控制項,便可在每次將項目加入至 ListBox 的同時維持效能。

命名空間:  System.Windows.Forms
組件:  System.Windows.Forms (在 System.Windows.Forms.dll 中)

語法

Visual Basic (宣告)
Public Sub BeginUpdate
Visual Basic (使用方式)
Dim instance As ListBox

instance.BeginUpdate()
C#
public void BeginUpdate()
Visual C++
public:
void BeginUpdate()
J#
public void BeginUpdate()
JScript
public function BeginUpdate()
備註

將多重項目加入至 ListBox 的慣用方法是使用 ListBox..::.ObjectCollection 類別的 AddRange 方法 (透過 ListBoxItems 屬性)。這可讓您在單一作業中將項目陣列加入至清單中。但如果您要使用 ListBox..::.ObjectCollection 類別的 Add 方法一次加入一個項目,可以使用 BeginUpdate 方法在每次項目加入至清單時,防止控制項重繪 ListBox。當您完成將項目加入至清單的工作後,請呼叫 EndUpdate 方法以便使 ListBox 進行重繪。這種加入項目的方法可防止當大量項目加入至清單時,出現 ListBox 閃動繪製。

範例

下列程式碼範例會在將五千個項目加入至 ListBox 時使用 BeginUpdateEndUpdate 方法。這個範例會要求已將名稱為 listBox1ListBox 控制項加入至 Form,並且已將這個方法置於呼叫該方法的表單中。

Visual Basic
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

C#
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();
}

Visual C++
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();
}
J#
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.get_Items().Add(("Item" + (new Integer(x)).ToString()));
    }

    // End the update process and force a repaint of the ListBox.
    listBox1.EndUpdate();
} //AddToMyListBox
JScript
function AddToMyListBox(){
    // Stop the ListBox from drawing while items are added.
    listBox1.BeginUpdate()

    // Loop through and add five thousand new items.
    for(var x = 0; x < 5000; x++)
        listBox1.Items.Add("Item " + x.ToString())
    // End the update process and force a repaint of the ListBox.
    listBox1.EndUpdate()
}

平台

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱.NET Framework 系統需求

版本資訊

.NET Framework

支援版本:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

支援版本:3.5、2.0
請參閱

參考

標記 :


Page view tracker