Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C#
Tip/Trick

Draw a Border Around Any C# Winform Control

Rate me:
Please Sign up or sign in to vote.
4.00/5 (17 votes)
9 Jan 2016CPOL 102.3K   11   7
Trick to draw a border of any color and width around any winform control

Introduction

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.

Background

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:

C#
public class MyGroupBox : GroupBox
{
   protected override void OnPaint(PaintEventArgs e)
   {
     base.OnPaint(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!!

License

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe proposed method is inaccurate Pin
a_pess9-Jan-16 16:23
a_pess9-Jan-16 16:23 
each control in dot net have its own way to draw border and accordingly you shall render each control separately
Question[My vote of 2] 2 because Pin
N. Henrik Lauridsen24-Apr-15 10:29
N. Henrik Lauridsen24-Apr-15 10:29 
QuestionWhy I voted you a 2? Pin
Tomaž Štih6-Mar-15 0:13
Tomaž Štih6-Mar-15 0:13 
GeneralMy vote is 5 Pin
Xniser7-Dec-14 1:55
Xniser7-Dec-14 1:55 
GeneralMy vote of 2 Pin
Member 1068509027-May-14 7:02
Member 1068509027-May-14 7:02 
QuestionNeed a sample Pin
i4inbox2-Nov-12 8:11
i4inbox2-Nov-12 8:11 
GeneralMy vote of 3 Pin
Yawness3-Jul-12 18:45
Yawness3-Jul-12 18:45 

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

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