TreeNodeCollection.AddRange(TreeNode[]) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将先前创建的一组树节点添加到集合中。
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
为 null
。
nodes
是另一 TreeView 的子级。
示例
下面的代码示例将 从 复制到TreeNodeCollection临时 Array,然后使用 方法将 数组的内容添加到另一个TreeViewAddRange数组。TreeView TreeNodeCollection使用 Clear 方法清除源TreeView中的 。 此示例要求有两 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,请使用 、 RemoveAt或 Clear 方法。