TreeNodeStates Enumeration
Defines constants that represent the possible states of a TreeNode.
This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.
Namespace: System.Windows.FormsAssembly: System.Windows.Forms (in System.Windows.Forms.dll)
| Member name | Description | |
|---|---|---|
| Checked | The node is checked. | |
| Default | The node is in its default state. | |
| Focused | The node has focus. | |
| Grayed | The node is disabled. | |
| Hot | The node is hot. This state occurs when the TreeView.HotTracking property is set to true and the mouse pointer is over the node. | |
| Indeterminate | The node in an indeterminate state. | |
| Marked | The node is marked. | |
| Selected | The node is selected. | |
| ShowKeyboardCues | The node should indicate a keyboard shortcut. |
This enumeration is used by the State property of the DrawTreeNodeEventArgs class. For more information, see the TreeView.DrawNode event.
The following example demonstrates how to customize a TreeView control using owner drawing. The TreeView control in the example displays optional node tags alongside the normal node labels. Node tags are specified using the TreeNode.Tag property. The TreeView control also uses custom colors, including a custom highlight color.
You can customize most of the TreeView colors by setting color properties, but the selection highlight color is not available as a property. Additionally, the default selection highlight rectangle extends only around a node label. Owner drawing must be used to draw the node tags and to draw a customized highlight rectangle large enough to include a node tag.
In the example, a handler for the TreeView.DrawNode event draws unselected nodes by calling methods of the DrawTreeNodeEventArgs class. These methods provide the default appearance for TreeView elements that do not need customization. The handler draws the node tags and the custom selection highlight manually.
For the complete example, see the TreeView.DrawNode reference topic.
' Draws a node. Private Sub myTreeView_DrawNode(ByVal sender As Object, _ ByVal e As DrawTreeNodeEventArgs) Handles myTreeView.DrawNode ' Draw the background and node text for a selected node. If (e.State And TreeNodeStates.Selected) <> 0 Then ' Draw the background of the selected node. The NodeBounds ' method makes the highlight rectangle large enough to ' include the text of a node tag, if one is present. e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node)) ' Retrieve the node font. If the node font has not been set, ' use the TreeView font. Dim nodeFont As Font = e.Node.NodeFont If nodeFont Is Nothing Then nodeFont = CType(sender, TreeView).Font End If ' Draw the node text. e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, _ e.Bounds.Left - 2, e.Bounds.Top) ' Use the default background and node text. Else e.DrawDefault = True End If ' If a node tag is present, draw its string representation ' to the right of the label text. If (e.Node.Tag IsNot Nothing) Then e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont, _ Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top) End If ' If the node has focus, draw the focus rectangle large, making ' it large enough to include the text of the node tag, if present. If (e.State And TreeNodeStates.Focused) <> 0 Then Dim focusPen As New Pen(Color.Black) Try focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot Dim focusBounds As Rectangle = NodeBounds(e.Node) focusBounds.Size = New Size(focusBounds.Width - 1, _ focusBounds.Height - 1) e.Graphics.DrawRectangle(focusPen, focusBounds) Finally focusPen.Dispose() End Try End If End Sub 'myTreeView_DrawNode
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.