TreeView.EndUpdate 方法

定義

啟用樹狀檢閱的重繪作業。

C#
public void EndUpdate ();

範例

下列程式碼範例會在 控制項中 TreeView 顯示客戶資訊。 根樹狀節點會顯示客戶名稱,而子樹狀節點會顯示指派給每個客戶的訂單編號。 在此範例中,每個客戶會顯示 1,000 個訂單 15 個。 的重新繪製 TreeView 會使用 BeginUpdateEndUpdate 方法來隱藏,並在建立和繪製 TreeNode 物件時 TreeView 顯示等候 Cursor 。 此範例會要求您有 Customer 可以保存物件集合的 Order 物件。 它也需要您在應用程式目錄中命名 MyWait.cur 的資料指標檔案,而且您已在 上 Form 建立 控制項的 TreeView 實例。

C#

// The basic Customer class.
public class Customer : System.Object
{
   private string custName = "";
   protected ArrayList custOrders = new ArrayList();

   public Customer(string customername)
   {
      this.custName = customername;
   }

   public string CustomerName
   {      
      get{return this.custName;}
      set{this.custName = value;}
   }

   public ArrayList CustomerOrders 
   {
      get{return this.custOrders;}
   }
} // End Customer class 

// The basic customer Order class.
public class Order : System.Object
{
   private string ordID = "";

   public Order(string orderid)
   {
      this.ordID = orderid;
   }

   public string OrderID
   {      
      get{return this.ordID;}
      set{this.ordID = value;}
   }
} // End Order class

// Create a new ArrayList to hold the Customer objects.
private ArrayList customerArray = new ArrayList(); 

private void FillMyTreeView()
{
   // Add customers to the ArrayList of Customer objects.
   for(int x=0; x<1000; x++)
   {
      customerArray.Add(new Customer("Customer" + x.ToString()));
   }

   // Add orders to each Customer object in the ArrayList.
   foreach(Customer customer1 in customerArray)
   {
      for(int y=0; y<15; y++)
      {
         customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));    
      }
   }

   // Display a wait cursor while the TreeNodes are being created.
   Cursor.Current = new Cursor("MyWait.cur");
        
   // Suppress repainting the TreeView until all the objects have been created.
   treeView1.BeginUpdate();

   // Clear the TreeView each time the method is called.
   treeView1.Nodes.Clear();

   // Add a root TreeNode for each Customer object in the ArrayList.
   foreach(Customer customer2 in customerArray)
   {
      treeView1.Nodes.Add(new TreeNode(customer2.CustomerName));
          
      // Add a child treenode for each Order object in the current Customer object.
      foreach(Order order1 in customer2.CustomerOrders)
      {
         treeView1.Nodes[customerArray.IndexOf(customer2)].Nodes.Add(
           new TreeNode(customer2.CustomerName + "." + order1.OrderID));
      }
   }

   // Reset the cursor to the default for all controls.
   Cursor.Current = Cursors.Default;

   // Begin repainting the TreeView.
   treeView1.EndUpdate();
}

備註

若要在專案一次新增至 TreeView 時維持效能,請呼叫 BeginUpdate 方法。 方法 BeginUpdate 會防止控制項繪製,直到 EndUpdate 呼叫 方法為止。

將專案新增至樹狀檢視控制項的慣用方法是使用 AddRange 方法,將樹狀節點專案的陣列加入樹狀檢視。 不過,如果您想要一次新增一個專案,請使用 BeginUpdate 方法來防止 TreeView 控制項在新增作業期間繪製。 若要允許控制項繼續繪製,請在所有樹狀節點都新增至樹狀檢視時呼叫 EndUpdate 方法。

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另請參閱