WinForm C # TreeView Changes the color of the selected node, and the selected node remains highlighted when the focus is lost.

Source: Internet
Author: User
When the user focus leaves the TreeView, the TreeView selection node is still highlighted, but the color is too light to be seen.
Here, rewrite the DrawMode () to control the color of the selected node.
Treeview.hideselection = FALSE; Allows the selected node to remain highlighted.

Reference: Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.treeview.hideselection (vs.80). aspx


Public Frmmain () {InitializeComponent ();
    Treeview1.hideselection = False;
    Self-drawn this.treeView1.DrawMode = Treeviewdrawmode.ownerdrawtext;
This.treeView1.DrawNode + = new Drawtreenodeeventhandler (Treeview1_drawnode); ///In the Drawing node event, draw the private void Treeview1_drawnode (object sender, Drawtreenodeeventargs e) {E.drawdefault = True, as you want;

    I use the default color here, just want to be in the TreeView lose focus when the selected node still highlighted return; if ((E.state & treenodestates.selected)!= 0) {//Demo for White E.graphics.fillrectangle (Brushes.darkblu

        E, e.node.bounds);
        Font Nodefont = E.node.nodefont; if (Nodefont = = null) Nodefont = (TreeView) sender).
        Font;
    E.graphics.drawstring (E.node.text, Nodefont, Brushes.white,rectangle.inflate (e.bounds, 2, 0));
    else {E.drawdefault = true;
        } if ((E.state & treenodestates.focused)!= 0) {using (Pen Focuspen = new Pen (color.black)) {Focuspen.dashstyle = System.Drawing.DrawinG2D.DashStyle.Dot;
            Rectangle focusbounds = e.node.bounds;
            Focusbounds.size = new Size (focusbounds.width-1, focusbounds.height-1);
        E.graphics.drawrectangle (Focuspen, focusbounds); }
    }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.