Editare

Partajați prin


TreeNodeCollection Class

Definition

Represents a collection of TreeNode objects in the TreeView control. This class cannot be inherited.

public ref class TreeNodeCollection sealed : System::Collections::ICollection, System::Web::UI::IStateManager
public sealed class TreeNodeCollection : System.Collections.ICollection, System.Web.UI.IStateManager
type TreeNodeCollection = class
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public NotInheritable Class TreeNodeCollection
Implements ICollection, IStateManager
Inheritance
TreeNodeCollection
Implements

Examples

The following example demonstrates how to programmatically add nodes to and remove them from a TreeNodeCollection. Notice that the Nodes and ChildNodes properties return a TreeNodeCollection object.


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

      // Use the Add and Remove methods to programmatically 
      // remove the Appendix C node and replace it with a new 
      // node. 
      LinksTreeView.Nodes.Remove(LinksTreeView.Nodes[3]);
      LinksTreeView.Nodes.Add(new TreeNode("New Appendix C"));

      // Use the AddAt and RemoveAt methods to programmatically 
      // remove the Chapter One node and replace it with a new node.
      LinksTreeView.Nodes[0].ChildNodes.RemoveAt(0);
      LinksTreeView.Nodes[0].ChildNodes.AddAt(0, new TreeNode("New Chapter One"));

      // Use the Clear method to remove all the child nodes of the 
      // Chapter Two node.
      LinksTreeView.Nodes[0].ChildNodes[1].ChildNodes.Clear();

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection 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="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>

    </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 Not IsPostBack Then

      ' Use the Add and Remove methods to programmatically 
      ' remove the Appendix C node and replace it with a new 
      ' node. 
      LinksTreeView.Nodes.Remove(LinksTreeView.Nodes(3))
      LinksTreeView.Nodes.Add(New TreeNode("New Appendix C"))

      ' Use the AddAt and RemoveAt methods to programmatically 
      ' remove the Chapter One node and replace it with a new node.
      LinksTreeView.Nodes(0).ChildNodes.RemoveAt(0)
      LinksTreeView.Nodes(0).ChildNodes.AddAt(0, New TreeNode("New Chapter One"))

      ' Use the Clear method to remove all the child nodes of the 
      ' Chapter Two node.
      LinksTreeView.Nodes(0).ChildNodes(1).ChildNodes.Clear()

    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeCollection 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="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>

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

Remarks

The TreeNodeCollection class is used to store and manage a collection of TreeNode objects in the TreeView control. The TreeView control uses the TreeNodeCollection class in two of its properties. It stores its root nodes in the Nodes property and its selected nodes in the CheckedNodes property. The TreeNodeCollection collection is also used for the ChildNodes property to store child nodes (if any).

The TreeNodeCollection class supports several ways to access the items in the collection:

  • Use the Item[] indexer to retrieve a TreeNode object directly at a specific zero-based index.

  • Use the GetEnumerator method to create an enumerator that can be used to iterate through the collection.

  • Use foreach (C#) or For Each (Visual Basic) to iterate through the collection.

  • Use the CopyTo method to copy the contents of the collection into an Array object.

You can programmatically manage the TreeNodeCollection by adding and removing TreeNode objects. To add nodes to the collection, use the Add or AddAt method. To remove nodes from the collection, use the Remove, RemoveAt, or Clear method.

Note

When the TreeView control is bound to a data source, the Nodes and ChildNodes collections are automatically populated each time binding occurs. Any changes to the collections between bindings will be lost. To retain these changes, either update the data source or manually rebuild the collection each time you bind.

The TreeNodeCollection contains properties and methods that allow you to retrieve information about the collection itself. To find out how many items are in the collection, use the Count property. If you want to determine whether the collection contains a certain TreeNode object, use the Contains method. To get the index of a TreeNode object in the collection, use the IndexOf method.

Constructors

TreeNodeCollection()

Initializes a new instance of the TreeNodeCollection class using the default values.

TreeNodeCollection(TreeNode)

Initializes a new instance of the TreeNodeCollection class using the specified parent node (or owner).

Properties

Count

Gets the number of items in the TreeNodeCollection object.

IsSynchronized

Gets a value indicating whether access to the TreeNodeCollection is synchronized (thread safe).

Item[Int32]

Gets the TreeNode object at the specified index in the TreeNodeCollection object.

SyncRoot

Gets an object that can be used to synchronize access to the TreeNodeCollection object.

Methods

Add(TreeNode)

Appends the specified TreeNode object to the end of the TreeNodeCollection object.

AddAt(Int32, TreeNode)

Inserts the specified TreeNode object in a TreeNodeCollection object at the specified index location.

Clear()

Empties the TreeNodeCollection object.

Contains(TreeNode)

Determines whether the specified TreeNode object is in the collection.

CopyTo(TreeNode[], Int32)

Copies all the items from the TreeNodeCollection object to a compatible one-dimensional array of TreeNode objects, starting at the specified index in the target array.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator()

Returns an enumerator that can be used to iterate through a TreeNodeCollection object.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(TreeNode)

Determines the index of the specified TreeNode object.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(TreeNode)

Removes the specified TreeNode object from the TreeNodeCollection object.

RemoveAt(Int32)

Removes the TreeNode object at the specified index location from the TreeNodeCollection object.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32)

Copies all the items from the TreeNodeCollection object to a compatible one-dimensional Array, starting at the specified index in the target array.

IStateManager.IsTrackingViewState

Gets a value indicating whether the TreeNodeCollection object is saving changes to its view state.

IStateManager.LoadViewState(Object)

Loads the TreeNodeCollection object's previously saved view state.

IStateManager.SaveViewState()

Saves the changes to view state to a Object.

IStateManager.TrackViewState()

Instructs the TreeNodeCollection to track changes to its view state.

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See also