System.Windows.Forms.TreeView Class

Displays a hierarchical collection of labeled items, each represented by a System.Windows.Forms.TreeNode.

See Also: TreeView Members

Syntax

[System.ComponentModel.Designer("System.Windows.Forms.Design.TreeViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
[System.ComponentModel.DefaultEvent("AfterSelect")]
[System.ComponentModel.DefaultProperty("Nodes")]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Windows.Forms.Docking(System.Windows.Forms.DockingBehavior.Ask)]
[System.Runtime.InteropServices.ComVisible(true)]
public class TreeView : Control

Remarks

The TreeView.Nodes collection holds all the System.Windows.Forms.TreeNode objects that are assigned to the System.Windows.Forms.TreeView control. The tree nodes in this collection are referred to as the root tree nodes. Any tree node that is subsequently added to a root tree node is referred to as a child node. Because each System.Windows.Forms.TreeNode can contain a collection of other System.Windows.Forms.TreeNode objects, you might find it difficult to determine your location in the tree structure when you iterate through the collection. You can parse the TreeNode.FullPath string by using the TreeView.PathSeparator string value to determine where a System.Windows.Forms.TreeNode label begins and ends.

You can display images next to the tree nodes by assigning an System.Windows.Forms.ImageList to the TreeView.ImageList property and referencing the index value of an System.Drawing.Image in the System.Windows.Forms.ImageList to assign that System.Drawing.Image. Use the following properties to assign images:

The images referenced by the TreeView.ImageIndex and TreeView.SelectedImageIndex property values are the default images displayed by all the tree nodes that are assigned to the TreeView.Nodes collection. Individual tree nodes can override the default images by setting the TreeNode.ImageIndex and TreeNode.SelectedImageIndex properties.

The state images displayed in the System.Windows.Forms.TreeView are 16 x 16 pixels by default. Setting the ImageList.ImageSize property of the TreeView.StateImageList will have no effect on how the images are displayed. However, the state images are resized according to the system DPI setting when the app.config file contains the following entry:

Example

<appSettings>
  <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>

Tree nodes can be expanded to display the next level of child tree nodes. The user can expand the System.Windows.Forms.TreeNode by clicking the plus-sign (+) button, if one is displayed next to the System.Windows.Forms.TreeNode, or you can expand the System.Windows.Forms.TreeNode by calling the TreeNode.Expand method. To expand all the 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-sign (-) button, if one is displayed next to the System.Windows.Forms.TreeNode. You can also call the TreeNode.Toggle method to alternate between the expanded and collapsed states.

Tree nodes can optionally display check boxes. 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.

Note:

Setting the TreeNode.Checked property from within the TreeView.BeforeCheck or TreeView.AfterCheck event causes the event to be raised multiple times and can result in unexpected behavior. For example, you might set the TreeNode.Checked property in the event handler when you are recursively updating the child nodes so that the user does not have to expand and check each one individually. To prevent the event from being raised multiple times, add logic to your event handler that only executes your recursive code if the TreeViewEventArgs.Action property of the System.Windows.Forms.TreeViewEventArgs is not set to TreeViewAction.Unknown. For an example of how to do this, see the Example section of the TreeView.AfterCheck or TreeView.BeforeCheck events.

You can change the appearance of the System.Windows.Forms.TreeView control by setting some of its display and style properties. Setting TreeView.ShowPlusMinus to true displays a plus-sign or minus-sign button next to each System.Windows.Forms.TreeNode that can be expanded or collapsed, respectively. Setting the TreeView.ShowRootLines property to true causes the System.Windows.Forms.TreeView to display lines that join all the root tree nodes together. You can display lines that connect child tree nodes to their root node by setting the TreeView.ShowLines property to true. Setting the TreeView.HotTracking property to true changes the appearance of the tree node labels as the mouse pointer passes over them. When hot-tracked, the tree node labels take on the appearance of a hyperlink. You can also completely customize the appearance of the System.Windows.Forms.TreeView control. To do this, set the TreeView.DrawMode property to a value other than TreeViewDrawMode.Normal and handle the TreeView.DrawNode event.

Note:

When setting the TreeView.CheckBoxes, TreeView.Scrollable, TreeView.ImageIndex, and TreeView.SelectedImageIndex properties at run time, the System.Windows.Forms.TreeView handle is recreated (see Control.RecreateHandle) to update the control's appearance. This causes all tree nodes to be collapsed, except for the selected System.Windows.Forms.TreeNode.

Requirements

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