System.Windows.Forms.TreeNode Class

Represents a node of a System.Windows.Forms.TreeView.

See Also: TreeNode Members

Syntax

[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeNodeConverter))]
[System.ComponentModel.DefaultProperty("Text")]
public class TreeNode : MarshalByRefObject, ICloneable, System.Runtime.Serialization.ISerializable

Remarks

The TreeNode.Nodes collection holds all the child System.Windows.Forms.TreeNode objects assigned to the current System.Windows.Forms.TreeNode. You can add, remove, or clone a System.Windows.Forms.TreeNode; when you do this, all child tree nodes are added, removed, or cloned. Each System.Windows.Forms.TreeNode can contain a collection of other System.Windows.Forms.TreeNode objects. This can make it difficult to determine where you are in the System.Windows.Forms.TreeView when iterating through the collection. To determine your location in a tree structure, use the TreeNode.FullPath property. The TreeNode.FullPath string can be parsed using the TreeView.PathSeparator string value to determine where a System.Windows.Forms.TreeNode label begins and ends.

The System.Windows.Forms.TreeNode label is set by setting the TreeNode.Text property explicitly. The alternative is to create the tree node using one of the TreeNode.#ctor constructors that has a string parameter that represents the TreeNode.Text property. The label is displayed next to the System.Windows.Forms.TreeNode image, if one is displayed.

To display images next to the tree nodes, assign an System.Windows.Forms.ImageList to the TreeView.ImageList property of the parent System.Windows.Forms.TreeView control and assign an System.Drawing.Image by referencing its index value in the System.Windows.Forms.ImageList property. Set the TreeNode.ImageIndex property to the index value of the System.Drawing.Image you want to display when the System.Windows.Forms.TreeNode is in an unselected state. Likewise, set the TreeNode.SelectedImageIndex property to the index value of the System.Drawing.Image you want to display when the System.Windows.Forms.TreeNode is selected.

Selecting specific tree nodes and iterating through the TreeView.Nodes collection can be achieved by using the following property values: TreeNode.FirstNode, TreeNode.LastNode, TreeNode.NextNode, TreeNode.PrevNode, TreeNode.NextVisibleNode, TreeNode.PrevVisibleNode. Assign the System.Windows.Forms.TreeNode returned by one of aforementioned properties to the TreeView.SelectedNode property to select that tree node in the System.Windows.Forms.TreeView control.

Tree nodes can be expanded to display the next level of child tree nodes. The user can expand the System.Windows.Forms.TreeNode by pressing the plus (+) button next to the System.Windows.Forms.TreeNode, if one is displayed, or you can expand the System.Windows.Forms.TreeNode by calling the TreeNode.Expand method. To expand all child tree node levels in the TreeNode.Nodes collection, call the TreeNode.ExpandAll method. You can collapse the child System.Windows.Forms.TreeNode level by calling the TreeNode.Collapse method, or the user can press the minus (-) button next to the System.Windows.Forms.TreeNode, if one is displayed. You can also call the TreeNode.Toggle method to alternate the System.Windows.Forms.TreeNode between the expanded and collapsed states.

Tree nodes can optionally display a check box. To display the check boxes, set the TreeView.CheckBoxes property of the System.Windows.Forms.TreeView to true. The TreeNode.Checked property is set to true for tree nodes that are in a checked state.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0