Click here to Skip to main content
11,413,961 members (77,147 online)
Click here to Skip to main content

Draw a Border around any C# Winform Control

, 20 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Trick to draw a border of any color and width around any winform control


Many times, we need to add/change the border of a WinForm control. In WPF, it's very easy and flexible to do so. But in Winforms, it might be tricky sometimes.


Most of the Winform controls have a property BorderStyle that allows for a border to be shown around that control. But the look and feel of that border cannot be changed. And there are controls (button, groupbox, etc.) that do not have this property. So there is no direct way of adding a Border around these controls.

Show Me the Code!

OK, enough talk! The following magic lines allow us to add a border around a control:

public class MyGroupBox : GroupBox
   protected override void OnPaint(PaintEventArgs e)
     ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
                                  Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
                                  Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
                                  Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
                                  Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset);

The ControlPaint class allows to add a border around a control. The above call can be invoked in a Paint event handler of a control. The ControlPaint class has many other useful methods (along with an overload of DrawBorder) that you can explore to learn more!

Happy coding!!


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


About the Author

Ankush Bansal
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

Question[My vote of 2] 2 because Pin
N. Henrik Lauridsen, 24-Apr-15 11:29
memberN. Henrik Lauridsen24-Apr-15 11:29 
QuestionWhy I voted you a 2? Pin
Tomaž Štih, 6-Mar-15 1:13
memberTomaž Štih6-Mar-15 1:13 
GeneralMy vote is 5 Pin
Xniser, 7-Dec-14 2:55
memberXniser7-Dec-14 2:55 
GeneralMy vote of 2 Pin
Member 10685090, 27-May-14 8:02
memberMember 1068509027-May-14 8:02 
QuestionNeed a sample Pin
i4inbox, 2-Nov-12 9:11
memberi4inbox2-Nov-12 9:11 
GeneralMy vote of 3 Pin
Yawness, 3-Jul-12 19:45
memberYawness3-Jul-12 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.2 | Last Updated 21 May 2012
Article Copyright 2012 by Ankush Bansal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid