TreeNodeCollection.Clear メソッド

定義

コレクションからすべてのツリー ノードを削除します。Removes all tree nodes from the collection.

public:
 virtual void Clear();
public virtual void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()

実装

次のコード例ではTreeNodeCollection 、をTreeViewから一時Arrayにコピーし、 AddRangeメソッドを使用して配列の内容TreeViewを別のに追加します。The following code example copies the TreeNodeCollection from a TreeView to a temporary Array, and then adds the contents of the array to another TreeView using the AddRange method. ClearソースTreeNodeCollection からのは、メソッドを使用して消去されます。TreeViewThe TreeNodeCollection from the source TreeView is cleared using the Clear method. この例では、オブジェクトのTreeView TreeNodeコレクションを持つ2つのコントロールが必要です。This example requires that you have two TreeView controls, one with a collection of TreeNode objects.

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

注釈

このメソッドを使用すると、ツリーノードのコレクション全体をツリービューからクリアできます。You can use this method to clear the entire collection of tree nodes from a tree view.

コレクションから個々のツリーノードを削除するにRemoveは、メソッドまたはRemoveAtメソッドを使用します。To remove an individual tree node from the collection, use the Remove or RemoveAt methods.

コレクションに新しいTreeNodeオブジェクトを追加するには、 AddAddRange、またInsertはメソッドを使用します。To add new TreeNode objects to the collection, use the Add, AddRange, or Insert methods.

適用対象