Click here to Skip to main content
15,921,989 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
How can I code the border and border colour of a groupbox in a Windows Form Application?
Posted
Updated 15-Mar-17 4:38am
v2

You can decorate any control by adding some graphical rendering to it. This is done by handling the event System.Windows.Forms.Control.Paint or overriding the virtual method System.Windows.Forms.Control.OnPaint in a derived class.

Don't repeat a common mistake: don't create an instance of System.Drawing.Graphics! — take an instance from event arguments.

Please see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

One finer hint: as you need a border, it will be very helpful to adjust the DisplayRectangle. You can override this virtual property in derived class to prevent overlapping of the children with your border which will take up some room near the edges.

Please see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.displayrectangle.aspx[^]. [EDIT: this link fixed — SA]

For more advanced information on rendering, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 25-Jan-12 15:33pm    
Good reply :)
Sergey Alexandrovich Kryukov 25-Jan-12 15:37pm    
Thank you, Espen.
--SA
 
Share this answer
 
By overriding the OnPaint(PaintEventArgs e) method

The source code for the default OnPaint method is available from Microsoft[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jan-12 16:14pm    
This is the best method, only there is a bit more to it. My 4 this time. :-)
--SA
Espen Harlinn 25-Jan-12 16:22pm    
Download existing code, copy the original GroupBox.OnPaint implmentation and modify it as desired - what more could op need? He'll get the correct paint behaviour without a lot of trial and error - as it's not as straight forward as one might believe. The original code shows how to handle the various control flags and adjust painting as required - and as far as I know that's not documented anywhere.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900