TreeNodeCollection.GetEnumerator 메서드

정의

TreeNodeCollection 개체를 반복하는 데 사용할 수 있는 열거자를 반환합니다.

public:
 virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator

반환

IEnumerator

TreeNodeCollection을 반복하는 데 사용할 수 있는 열거자입니다.

구현

예제

다음 예제에서는 사용 하는 방법에 설명 합니다 GetEnumerator 의 루트 노드를 포함 하는 열거자를 만드는 메서드를는 TreeView 컨트롤입니다.


<%@ 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)
    {

      // Use the GetEnumerator method to create an enumerator 
      // that contains the root node data.
      IEnumerator nodeEnumerator = LinksTreeView.Nodes.GetEnumerator();

      // Iterate through the enumerator to display the root nodes.
      while (nodeEnumerator.MoveNext())
      {

        Message.Text += ((TreeNode)(nodeEnumerator.Current)).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 GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection GetEnumerator 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

      ' Use the GetEnumerator method to create an enumerator 
      ' that contains the root node data.
      Dim nodeEnumerator As IEnumerator = LinksTreeView.Nodes.GetEnumerator()

      ' Iterate through the enumerator to display the root nodes.
      While nodeEnumerator.MoveNext()

        Message.Text &= (CType(nodeEnumerator.Current, TreeNode)).Text & "<br />"

      End While

    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 GetEnumerator Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection GetEnumerator 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>

설명

사용 합니다 GetEnumerator 방법을 쉽게 반복할 수의 각 항목을 가져올 수 있는 열거자를 만들 수는 TreeNodeCollection합니다. 열거자에서 현재 가리키는 항목을 사용 합니다 IEnumerator.Current 속성입니다. 사용 된 IEnumerator.MoveNext 다음 항목으로 이동 하는 방법입니다. 컬렉션의 시작 부분에 열거자를 다시 이동 해야 할 경우 사용 된 IEnumerator.Reset 메서드.

참고

열거자를 만들거나 사용 후 합니다 IEnumerator.Reset 호출 해야 합니다는 IEnumerator.MoveNext 메서드. 항목을 표시 하는 고, 그렇지는 IEnumerator.Current 속성 정의 되지 않습니다.

대신 사용할 수도 있습니다는 CopyTo 메서드를는 System.Array 컬렉션의 항목에 액세스 하는 개체입니다. 또 다른 방법은 사용 하는 것을 foreach 또는 For Each 컬렉션을 반복 하는 구조입니다.

적용 대상

추가 정보