TreeNodeCollection.GetEnumerator Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce un enumeratore che può essere utilizzato per scorrere un TreeNodeCollection oggetto .
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
Restituisce
Enumeratore che può essere usato per scorrere .TreeNodeCollection
Implementazioni
Esempio
Nell'esempio seguente viene illustrato come usare il GetEnumerator metodo per creare un enumeratore contenente i nodi radice del TreeView controllo.
<%@ 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>
Commenti
Usare il GetEnumerator metodo per creare un enumeratore che può essere facilmente iterato attraverso per ottenere ogni elemento in TreeNodeCollection. Per ottenere l'elemento a cui punta attualmente nell'enumeratore, utilizzare la IEnumerator.Current proprietà . Utilizzare il IEnumerator.MoveNext metodo per passare all'elemento successivo. Se è necessario spostare nuovamente l'enumeratore all'inizio della raccolta, usare il IEnumerator.Reset metodo .
Annotazioni
Dopo aver creato un enumeratore o aver usato il IEnumerator.Reset metodo , è necessario chiamare il IEnumerator.MoveNext metodo . In caso contrario, l'elemento rappresentato dalla IEnumerator.Current proprietà non è definito.
In alternativa, è anche possibile utilizzare il CopyTo metodo per creare un System.Array oggetto che può essere utilizzato per accedere agli elementi nella raccolta. Un'altra alternativa consiste nell'usare una foreach struttura o For Each per scorrere la raccolta.