共用方式為


TreeNodeCollection.AddRange(TreeNode[]) 方法

定義

將先前建立的樹狀節點陣列加入集合中。

public:
 virtual void AddRange(cli::array <System::Windows::Forms::TreeNode ^> ^ nodes);
public:
 virtual void AddRange(... cli::array <System::Windows::Forms::TreeNode ^> ^ nodes);
public virtual void AddRange (System.Windows.Forms.TreeNode[] nodes);
public virtual void AddRange (params System.Windows.Forms.TreeNode[] nodes);
abstract member AddRange : System.Windows.Forms.TreeNode[] -> unit
override this.AddRange : System.Windows.Forms.TreeNode[] -> unit
Public Overridable Sub AddRange (nodes As TreeNode())
Public Overridable Sub AddRange (ParamArray nodes As TreeNode())

參數

nodes
TreeNode[]

TreeNode 物件的陣列,表示要加入至集合中的樹狀節點。

例外狀況

nodesnull

nodes 是另一個 TreeView 的子系。

範例

下列程式碼範例會 TreeNodeCollectionTreeView 複製到暫存 Array 的 ,然後使用 方法將 陣列的內容加入另一個 TreeViewAddRange 陣列中。 來源的 TreeViewTreeNodeCollection 使用 Clear 方法清除 。 此範例需要您有兩 TreeView 個控制項,一個具有 物件的集合 TreeNode

private:
   void MyButtonAddAllClick( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      
      // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
      TreeNodeCollection^ myTreeNodeCollection = myTreeViewBase->Nodes;
      
      // Create an array of 'TreeNodes'.
      array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(myTreeViewBase->Nodes->Count);
      
      // Copy the tree nodes to the 'myTreeNodeArray' array.
      myTreeViewBase->Nodes->CopyTo( myTreeNodeArray, 0 );
      
      // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
      myTreeViewBase->Nodes->Clear();
      
      // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
      myTreeViewCustom->Nodes->AddRange( myTreeNodeArray );
   }
private void MyButtonAddAllClick(object sender, EventArgs e)
{
   // Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
   TreeNodeCollection myTreeNodeCollection = myTreeViewBase.Nodes;
   // Create an array of 'TreeNodes'.
   TreeNode[] myTreeNodeArray = new TreeNode[myTreeViewBase.Nodes.Count];
   // Copy the tree nodes to the 'myTreeNodeArray' array.
   myTreeViewBase.Nodes.CopyTo(myTreeNodeArray,0);
   // Remove all the tree nodes from the 'myTreeViewBase' TreeView.
   myTreeViewBase.Nodes.Clear();
   // Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
   myTreeViewCustom.Nodes.AddRange(myTreeNodeArray);
}
Private Sub MyButtonAddAllClick(sender As Object, e As EventArgs)
   ' Get the 'myTreeNodeCollection' from the 'myTreeViewBase' TreeView.
   Dim myTreeNodeCollection As TreeNodeCollection = myTreeViewBase.Nodes
   ' Create an array of 'TreeNodes'.
   Dim myTreeNodeArray(myTreeViewBase.Nodes.Count-1) As TreeNode
   ' Copy the tree nodes to the 'myTreeNodeArray' array.
   myTreeViewBase.Nodes.CopyTo(myTreeNodeArray, 0)
   ' Remove all the tree nodes from the 'myTreeViewBase' TreeView.
   myTreeViewBase.Nodes.Clear()
   ' Add the 'myTreeNodeArray' to the 'myTreeViewCustom' TreeView.
      myTreeViewCustom.Nodes.AddRange(myTreeNodeArray)
End Sub

備註

TreeNode陣列中包含的 nodes 物件會附加至集合結尾。

您可以使用 AddRange 方法快速將先前建立 TreeNode 的物件群組新增至集合,而不是使用 Add 方法手動將每個 TreeNode 物件新增至集合。

若要移除 TreeNode 您先前新增的 Remove ,請使用 、 RemoveAtClear 方法。

適用於

另請參閱