Export (0) Print
Expand All

TreeView.DrawMode Property

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

Gets or sets the mode in which the control is drawn.

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

public TreeViewDrawMode DrawMode { get; set; }
/** @property */
public TreeViewDrawMode get_DrawMode ()

/** @property */
public void set_DrawMode (TreeViewDrawMode value)

public function get DrawMode () : TreeViewDrawMode

public function set DrawMode (value : TreeViewDrawMode)

Property Value

One of the TreeViewDrawMode values. The default is TreeViewDrawMode.Normal.

Exception typeCondition

InvalidEnumArgumentException

The property value is not a valid TreeViewDrawMode value.

The nodes of a TreeView are typically drawn by the operating system. The DrawMode property lets you customize the appearance of the nodes. To do this, set DrawMode to TreeViewDrawMode.OwnerDrawAll or TreeViewDrawMode.OwnerDrawText and provide a handler for the DrawNode event. This is called owner drawing.

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.

For the complete example, see the DrawNode reference topic.

public TreeViewOwnerDraw()
{
    // Create and initialize the TreeView control.
    myTreeView = new TreeView();
    myTreeView.Dock = DockStyle.Fill;
    myTreeView.BackColor = Color.Tan;
    myTreeView.CheckBoxes = true;

    // Add nodes to the TreeView control.
    TreeNode node;
    for (int x = 1; x < 4; ++x)
    {
        // Add a root node to the TreeView control.
        node = myTreeView.Nodes.Add(String.Format("Task {0}", x));
        for (int y = 1; y < 4; ++y)
        {
            // Add a child node to the root node.
            node.Nodes.Add(String.Format("Subtask {0}", y));
        }
    }
    myTreeView.ExpandAll();

    // Add tags containing alert messages to a few nodes 
    // and set the node background color to highlight them.
    myTreeView.Nodes[1].Nodes[0].Tag = "urgent!";
    myTreeView.Nodes[1].Nodes[0].BackColor = Color.Yellow;
    myTreeView.SelectedNode = myTreeView.Nodes[1].Nodes[0];
    myTreeView.Nodes[2].Nodes[1].Tag = "urgent!";
    myTreeView.Nodes[2].Nodes[1].BackColor = Color.Yellow;

    // Configure the TreeView control for owner-draw and add
    // a handler for the DrawNode event.
    myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawText;
    myTreeView.DrawNode += 
        new DrawTreeNodeEventHandler(myTreeView_DrawNode);

    // Add a handler for the MouseDown event so that a node can be 
    // selected by clicking the tag text as well as the node text.
    myTreeView.MouseDown += new MouseEventHandler(myTreeView_MouseDown);

    // Initialize the form and add the TreeView control to it.
    this.ClientSize = new Size(292, 273);
    this.Controls.Add(myTreeView);
}

public TreeViewOwnerDraw()
{
    // Create and initialize the TreeView control.
    myTreeView = new TreeView();
    myTreeView.set_Dock(DockStyle.Fill);
    myTreeView.set_BackColor(Color.get_Tan());
    myTreeView.set_CheckBoxes(true);
    // Add nodes to the TreeView control.
    TreeNode node;
    for (int x = 1; x < 4; ++x) {
        // Add a root node to the TreeView control.
        node = myTreeView.get_Nodes().Add(String.Format("Task {0}",
            (Int32)x));
        for (int y = 1; y < 4; ++y) {
            // Add a child node to the root node.
            node.get_Nodes().Add(String.Format("Subtask {0}", (Int32)y));
        }
    }
    myTreeView.ExpandAll();
    // Add tags containing alert messages to a few nodes 
    // and set the node background color to highlight them.
    myTreeView.get_Nodes().get_Item(1).get_Nodes().get_Item(0).
        set_Tag("urgent!");
    myTreeView.get_Nodes().get_Item(1).get_Nodes().get_Item(0).
        set_BackColor(Color.get_Yellow());
    myTreeView.set_SelectedNode(myTreeView.get_Nodes().get_Item(1).
        get_Nodes().get_Item(0));
    myTreeView.get_Nodes().get_Item(2).get_Nodes().get_Item(1).
        set_Tag("urgent!");
    myTreeView.get_Nodes().get_Item(2).get_Nodes().get_Item(1).
        set_BackColor(Color.get_Yellow());

    // Configure the TreeView control for owner-draw and add
    // a handler for the DrawNode event.
    myTreeView.set_DrawMode(TreeViewDrawMode.OwnerDrawText);
    myTreeView.add_DrawNode(new DrawTreeNodeEventHandler(
        myTreeView_DrawNode));
    // Add a handler for the MouseDown event so that a node can be 
    // selected by clicking the tag text as well as the node text.
    myTreeView.add_MouseDown(new MouseEventHandler(myTreeView_MouseDown));
    // Initialize the form and add the TreeView control to it.
    this.set_ClientSize(new Size(292, 273));
    this.get_Controls().Add(myTreeView);
} //TreeViewOwnerDraw

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:
© 2014 Microsoft