TreeNodeCollection.CopyTo(TreeNode[], Int32) 方法

定义

从目标数组的指定索引开始,将 TreeNodeCollection 对象中的所有项复制到 TreeNode 对象的一维兼容数组。

public:
 void CopyTo(cli::array <System::Web::UI::WebControls::TreeNode ^> ^ nodeArray, int index);
public void CopyTo (System.Web.UI.WebControls.TreeNode[] nodeArray, int index);
member this.CopyTo : System.Web.UI.WebControls.TreeNode[] * int -> unit
Public Sub CopyTo (nodeArray As TreeNode(), index As Integer)

参数

nodeArray
TreeNode[]

TreeNode 对象的从零开始的数组,它接收从 TreeNodeCollection 复制的项。

index
Int32

目标数组中开始接收复制内容的位置。

示例

下面的示例演示如何使用 CopyTo 方法将 的值 TreeNodeCollection 复制到数组中。 请注意, Nodes 属性返回 对象 TreeNodeCollection


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // If the TreeView control contains any root nodes, display the 
    // text value of each node. 
    if (LinksTreeView.Nodes.Count > 0)
    {

      // Declare an array of TreeNode objects.
      TreeNode[] RootNodeArray = new TreeNode[LinksTreeView.Nodes.Count];

      // Use the CopyTo method to copy the root nodes into the array.
      LinksTreeView.Nodes.CopyTo(RootNodeArray, 0);

      // Display the root nodes.
      foreach (TreeNode node in RootNodeArray)
      {

        Message.Text += node.Text + "<br />";

      }

    }
    else
    {

      Message.Text = "The TreeView control does not have any nodes.";

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection CopyTo Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection CopyTo Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        runat="server">
         
        <LevelStyles>
        
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Size="8pt"/>
             
        </LevelStyles>
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            Expanded="true">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
            <asp:TreeNode Text="Chapter Two">
            
              <asp:TreeNode Text="Section 2.0">
              
                <asp:TreeNode Text="Topic 2.0.1">
                
                  <asp:TreeNode Text="Subtopic 1"/>
                  <asp:TreeNode Text="Subtopic 2"/>
                
                </asp:TreeNode>
                <asp:TreeNode Text="Topic 2.0.2"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
          
          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />
        
        </Nodes>

      </asp:TreeView>
      
      <br /><br />
      
      <asp:Label id="Message"
        runat="server"/>

    </form>
  </body>
</html>

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' If the TreeView control contains any root nodes, display the 
    ' text value of each node. 
    If LinksTreeView.Nodes.Count > 0 Then

      ' Declare an array of TreeNode objects.
      Dim RootNodeArray(LinksTreeView.Nodes.Count - 1) As TreeNode

      ' Use the CopyTo method to copy the root nodes into the array.
      LinksTreeView.Nodes.CopyTo(RootNodeArray, 0)

      ' Display the root nodes.
      Dim node As TreeNode

      For Each node In RootNodeArray

        Message.Text &= node.Text & "<br />"

      Next node

    Else

      Message.Text = "The TreeView control does not have any nodes."

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection CopyTo Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection CopyTo Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        runat="server">
         
        <LevelStyles>
        
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Bold="true" 
            Font-Size="12pt" 
            ForeColor="DarkGreen"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-Bold="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="5" 
            Font-UnderLine="true" 
            Font-Size="10pt"/>
          <asp:TreeNodeStyle ChildNodesPadding="10" 
            Font-Size="8pt"/>
             
        </LevelStyles>
         
        <Nodes>
        
          <asp:TreeNode Text="Table of Contents"
            Expanded="true">
             
            <asp:TreeNode Text="Chapter One">
            
              <asp:TreeNode Text="Section 1.0">
              
                <asp:TreeNode Text="Topic 1.0.1"/>
                <asp:TreeNode Text="Topic 1.0.2"/>
                <asp:TreeNode Text="Topic 1.0.3"/>
              
              </asp:TreeNode>
              
              <asp:TreeNode Text="Section 1.1">
              
                <asp:TreeNode Text="Topic 1.1.1"/>
                <asp:TreeNode Text="Topic 1.1.2"/>
                <asp:TreeNode Text="Topic 1.1.3"/>
                <asp:TreeNode Text="Topic 1.1.4"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
            <asp:TreeNode Text="Chapter Two">
            
              <asp:TreeNode Text="Section 2.0">
              
                <asp:TreeNode Text="Topic 2.0.1">
                
                  <asp:TreeNode Text="Subtopic 1"/>
                  <asp:TreeNode Text="Subtopic 2"/>
                
                </asp:TreeNode>
                <asp:TreeNode Text="Topic 2.0.2"/>
              
              </asp:TreeNode>
            
            </asp:TreeNode>
            
          </asp:TreeNode>
          
          <asp:TreeNode Text="Appendix A" />
          <asp:TreeNode Text="Appendix B" />
          <asp:TreeNode Text="Appendix C" />
        
        </Nodes>
        
      </asp:TreeView>
      
      <br /><br />
      
      <asp:Label id="Message"
        runat="server"/>

    </form>
  </body>
</html>

注解

CopyTo使用 方法将 的内容TreeNodeCollection复制到指定的从零开始System.Array的对象中。 从目标数组的指定索引处开始复制项。 System.Array通过 对象,可以使用数组语法访问 中的TreeNodeCollection项。

作为替代方法,还可以使用 GetEnumerator 方法创建可用于访问集合中的项的枚举器。

适用于

另请参阅