Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Node Property
Collapse the table of content
Expand the table of content

DrawTreeNodeEventArgs.Node Property

Note: This property is new in the .NET Framework version 2.0.

Gets the TreeNode to draw.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public TreeNode Node { get; }
/** @property */
public TreeNode get_Node ()

public function get Node () : TreeNode

Property Value

The TreeNode to draw.

Use this property to access the TreeNode object to draw. This is useful when the State property does not provide adequate information to meet your needs. The State property provides only basic state information that you can use, for example, to determine whether a node is selected, checked, or focused. The Node property, on the other hand, allows you to access all members of the TreeNode object. You must access the node directly, for example, when you want to determine its expansion state.

The following code example demonstrates how to customize a TreeView control using owner drawing. The TreeView control in the example displays optional node tags alongside the standard 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 the node tags and the custom selection highlight manually. Unselected nodes do not need customization. For these, the DrawDefault property is set to true so that they will be drawn by the operating system.

For the complete example, see the DrawTreeNodeEventArgs overview reference topic.

// Draws a node.
private void myTreeView_DrawNode(
    object sender, DrawTreeNodeEventArgs e)
{
    // Draw the background and node text for a selected node.
    if ((e.State & TreeNodeStates.Selected) != 0)
    {
        // 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.
        Font nodeFont = e.Node.NodeFont;
        if (nodeFont == null) nodeFont = ((TreeView)sender).Font;

        // Draw the node text.
        e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,
            Rectangle.Inflate(e.Bounds, 2, 0));
    }

    // Use the default background and node text.
    else 
    {
        e.DrawDefault = true;
    }

    // If a node tag is present, draw its string representation 
    // to the right of the label text.
    if (e.Node.Tag != null)
    {
        e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont,
            Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top);
    }

    // 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 & TreeNodeStates.Focused) != 0)
    {
        using (Pen focusPen = new Pen(Color.Black))
        {
            focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            Rectangle focusBounds = NodeBounds(e.Node);
            focusBounds.Size = new Size(focusBounds.Width - 1, 
            focusBounds.Height - 1);
            e.Graphics.DrawRectangle(focusPen, focusBounds);
        }
    }
}

// Draws a node.
private void myTreeView_DrawNode(Object sender, DrawTreeNodeEventArgs e)
{
    // Draw the background and node text for a selected node.
    if ((int)(e.get_State() & TreeNodeStates.Selected) != 0) {
        // 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.get_Graphics().FillRectangle(Brushes.get_Green(),
            NodeBounds(e.get_Node()));
        // Retrieve the node font. If the node font has not been set,
        // use the TreeView font.
        Font nodeFont = e.get_Node().get_NodeFont();
        if (nodeFont == null) {
            nodeFont = ((TreeView)sender).get_Font();
        }
        // Draw the node text.
        e.get_Graphics().DrawString(e.get_Node().get_Text(),
            nodeFont, Brushes.get_White(),
            RectangleF.op_Implicit(Rectangle.Inflate(e.get_Bounds(), 2, 0)));
    }
    // Use the default background and node text.
    else {
        e.set_DrawDefault(true);
    }
    // If a node tag is present, draw its string representation 
    // to the right of the label text.
    if (e.get_Node().get_Tag() != null) {
        e.get_Graphics().DrawString(e.get_Node().get_Tag().ToString(),
            tagFont, Brushes.get_Yellow(), e.get_Bounds().get_Right() + 2,
            e.get_Bounds().get_Top());
    }
    // 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 ((int)(e.get_State() & TreeNodeStates.Focused) != 0) { 
        Pen focusPen = new Pen(Color.get_Black());
        try {
            focusPen.set_DashStyle(System.Drawing.Drawing2D.DashStyle.Dot);
            Rectangle focusBounds = NodeBounds(e.get_Node());
            focusBounds.set_Size(new Size(focusBounds.get_Width() - 1,
                focusBounds.get_Height() - 1));
            e.get_Graphics().DrawRectangle(focusPen, focusBounds);
        }
        finally {
            focusPen.Dispose();
        }            
    }
} //myTreeView_DrawNode

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft