TreeNodeCollection.Clear 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從集合中移除所有樹狀節點。
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 的 ,然後使用 方法將 陣列的內容加入另一個 TreeView AddRange 陣列中。 來源的 TreeView 會 TreeNodeCollection 使用 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
備註
您可以使用這個方法,從樹狀檢視清除整個樹狀節點集合。
若要從集合中移除個別樹狀節點,請使用 Remove 或 RemoveAt 方法。
若要將新 TreeNode 物件加入至集合,請使用 Add 、 AddRange 或 Insert 方法。