TreeView

A TreeView control displays a collection of items in a hierarchical view. Tree views are very common in the computer world, as seen in Windows Explorer, Microsoft Outlook, and Outlook Express, the Solution Explorer in Visual Studio .NET, and countless other applications.

Each item in a tree view is encapsulated within a TreeNode object. Each TreeNode can have zero, one, or many child nodes. The Nodes property of the tree view represents the collection of TreeNode objects that comprise the root, or top level, nodes in the hierarchy. Each node in turn has its own Nodes collection that contains all of that node's child nodes, and so on down the hierarchy. Your program can iterate through each of these collections, recursively if necessary, to walk the entire tree structure, as demonstrated shortly.

A tree view can have only a single node selected at any given time. The user can select a node either by clicking on the node with the mouse or using the arrow keys on the keyboard to move focus up and down the hierarchy. A node can be selected programmatically by setting the TreeView.SelectedNode property, and the currently selected node can be determined by getting the value of this property.

Since the SelectedNode property returns only the last node in the full path for that node in the hierarchy, which node in the tree is actually selected can be ambiguous if you rely only on the Text property of the TreeNode returned by the SelectedNode property. Use the FullPath property of ...

Get Programming .NET Windows Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.