C # winform Treeview changes the color of the selected node. when the focus is lost, the selected node is highlighted.

Source: Internet
Author: User
When the user focus leaves the Treeview, the selected node of the Treeview is still highlighted, but the color is too light to be seen.
Override drawmode () here to control the color of the selected node.
Treeview. hideselection = false; the selected node can be highlighted.
Refer: 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 draw node event, draw by yourself
private void treeview1_drawnode ( Object sender, drawtreenodeeventargs e)
{< br> E. drawdefault = true ; /// use the default color here, you only need to highlight the selected node when the Treeview loses focus
return ;

If (E. State & treenodestates. Selected )! = 0 )
{< br> /// White text with green background
E. graphics. fillrectangle (brushes. darkblue, 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 );
}
}

}

URL: http://greatverve.cnblogs.com/archive/2012/03/21/treeview-HideSelection.html

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.