TreeNodeCollection.GetEnumerator 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回列舉值,可用來逐一查看 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
傳回
列舉值,可用來逐一查看 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
結構逐一查看集合。