C # Form Control GroupBox Modify border color

Source: Internet
Author: User
Tags border color

Tag: His box height modifies order body override CSDN ref

Control group box The default color of the border is white, which in many cases appears less prominent. However, the default list of properties does not provide the appropriate interface. So you can only redraw events with the help of.

Many on the web said that using the OnPaint event, but I did not find it in the list of events, should be hidden too deep (need to use the Override keyword to rewrite). I have directly used the Paint event here and can achieve its effect.

Thank you: http://blog.csdn.net/haoduo123456789001/article/details/51083223

     Public Partial classTestform:form { PublicTestform () {InitializeComponent ();  This. groupbox1.paint + =Groupbox_paint;  This. groupbox2.paint + =Groupbox_paint; }        voidGroupbox_paint (Objectsender, PaintEventArgs e) {GroupBox Gbox=(GroupBox) sender;            E.graphics.clear (Gbox.backcolor); E.graphics.drawstring (Gbox.text, Gbox.font, brushes.red,Ten,1); varVsize =e.graphics.measurestring (Gbox.text, Gbox.font); E.graphics.drawline (pens.red,1, Vsize.height/2,8, Vsize.height/2); E.graphics.drawline (pens.red, Vsize.width+8, Vsize.height/2, Gbox.width-2, Vsize.height/2); E.graphics.drawline (pens.red,1, Vsize.height/2,1, Gbox.height-2); E.graphics.drawline (pens.red,1, Gbox.height-2, Gbox.width-2, Gbox.height-2); E.graphics.drawline (pens.red, Gbox.width-2, Vsize.height/2, Gbox.width-2, Gbox.height-2); }        Private voidTestform_load (Objectsender, EventArgs e) {                    }    }

Of course, if you would like to, see the following method of using OnPaint to implement.

First, you need to create a component of your own (similar to a custom control):

Once added, switch to Code view.

Change the inheritance relationship to:

     Public Partial class  //Component    {        public  mygroupbox ()        {            InitializeComponent ();        }          Public Mygroupbox (IContainer container)        {            container. ADD (this);            InitializeComponent ();        }
//rewrite }

Then rewrite the OnPaint () method:

        //rewrite        protected Override voidOnPaint (PaintEventArgs e) {varVsize = e.graphics.measurestring ( This. Text, This.                Font); E.graphics.clear ( This.            BackColor); E.graphics.drawstring ( This. Text, This. Font,NewSolidBrush ( This. ForeColor),Ten,1); E.graphics.drawline (Pens.black,1, Vsize.height/2,8, Vsize.height/2); E.graphics.drawline (Pens.black, Vsize.width+8, Vsize.height/2, This. Width-2, Vsize.height/2); E.graphics.drawline (Pens.black,1, Vsize.height/2,1, This. Height-2); E.graphics.drawline (Pens.black,1, This. Height-2, This. Width-2, This. Height-2); E.graphics.drawline (Pens.black, This. Width-2, Vsize.height/2, This. Width-2, This. Height-2); }

Press F6 to generate it, then you can find it in the toolbar, and then you don't need to use the previous GroupBox. Once

You can even expose the border color to yourself by means of a control property.

     Public Partial classMygroupbox:groupbox//Component    {        PrivateColor Mbordercolor =Color.Black;
[Browsable (true), Description ("Border Color"), Category ("Custom Grouping")] PublicColor bordercolor {Get{returnMbordercolor;} Set{Mbordercolor =value;} } PublicMygroupbox () {InitializeComponent (); } PublicMygroupbox (IContainer container) {container. ADD ( This); InitializeComponent (); } //rewrite protected Override voidOnPaint (PaintEventArgs e) {varVsize = e.graphics.measurestring ( This. Text, This. Font); E.graphics.clear ( This. BackColor); E.graphics.drawstring ( This. Text, This. Font,NewSolidBrush ( This. ForeColor),Ten,1); Pen Vpen=NewPen ( This. Mbordercolor);//use attribute color to draw border colorE.graphics.drawline (Vpen,1, Vsize.height/2,8, Vsize.height/2); E.graphics.drawline (Vpen, Vsize.width+8, Vsize.height/2, This. Width-2, Vsize.height/2); E.graphics.drawline (Vpen,1, Vsize.height/2,1, This. Height-2); E.graphics.drawline (Vpen,1, This. Height-2, This. Width-2, This. Height-2); E.graphics.drawline (Vpen, This. Width-2, Vsize.height/2, This. Width-2, This. Height-2); } }

[http://www.cnblogs.com/CUIT-DX037/]

C # Form Control GroupBox Modify border color

Related Article

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.