How to change Panel Border Color
Solution 1
If you don't want to make a custom panel as suggested in @Sinatr's answer you can draw the border yourself:
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Solid);
}
Solution 2
You can create own Panel
class and draw border in the client area:
[System.ComponentModel.DesignerCategory("Code")]
public class MyPanel : Panel
{
public MyPanel()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new SolidBrush(BackColor))
e.Graphics.FillRectangle(brush, ClientRectangle);
e.Graphics.DrawRectangle(Pens.Yellow, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
}
Solution 3
If you don't want to go to the trouble of sub-classing a panel, you can just create another panel 2 pixels bigger in each dimension, make it the border color and sit it directly behind the panel that needs a border. This is just a few clicks in the IDE ...
Solution 4
I found this post useful
I also set the padding of the panel to the thickness of the border so that controls inside the panel don't overlap the border and hide it. In my case, I wasn't using the padding otherwise so it was a good solution, but things might get more tricky if you also plan on using the padding for more than just showing the border...
Solution 5
This also worked for me:
private void HCp_Paint(object sender, PaintEventArgs e)
{
Panel p = sender as Panel;
ControlPaint.DrawBorder(e.Graphics, p.DisplayRectangle, Color.Yellow, ButtonBorderStyle.Solid);
}
The problem of the border style is due to the ButtomBorderStyle option "Inset". By selecting "ButtonBorderStyle.Solid" you get a single line (also dotted, dashed... are available).
For many panels, I agree that the best solution is to create your own class that inherits from Panel and overwrite the Paint method...
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on May 05, 2021Comments
-
Admin about 3 years
In the properties of a
Panel
I have set the border style toFixed Single
.
When I am running my application it has the color gray. I don't know how to change the border color.I have tried this in the
Paint
event handler of the panel:private void HCp_Paint(object sender, PaintEventArgs e) { Panel p = sender as Panel; ControlPaint.DrawBorder(e.Graphics, p.DisplayRectangle, Color.Yellow, ButtonBorderStyle.Inset); }
This displays the border like this:
but I want a fixed single border like this:
How I make the border in yellow?
-
DonBoitnott over 10 yearsI often put border painting in
OnPaintBackground
just to keep the frequency down. -
Admin over 10 yearsHi Sinatr, can you please explain where to write this code? I am not getting where to write this code I am new, pls tell the entire process.
-
JPProgrammer over 9 yearsThis solution works, but if you have a control in the panel that is set to dock.fill (for example), it overlaps on top of the rectangle and the rectangle is hidden. Anyway to set the panel's "usable space" to within the rectangle so that controls within don't overlap the rectangle?
-
Sinatr over 9 years@JPProgrammer,
Dock.Fill
can be easily replaced with anchoring (set content control location and anchorLeft | Top | Right | Bottom
), so that content will not cover drawn in client region border lines. Do not useDock.Fill
. -
JPProgrammer over 9 years@Sinatr - I just gave setting the
Location
andAnchor
a try but couldn't get it to work easily. Setting the location allows the left and top borders to show, but the right and bottom borders get overlapped. I guess I would have set the size (and continually resize the content whenever the panel resized) to get it to show the right and bottom borders? In my case I set the padding of the panel to the width of the border and got it to work (works usingDock.Fill
orAnchor
). -
Artholl about 9 yearsThis is great solution, but there is small problem when you have
ScrollBar
in yourPanel
. Then you should make also handler forScroll
event. Same like forResize
. -
Elmue over 7 yearsThis answer is WRONG. I don't undestand why I find wo many wrong answers on Stackoverflow that have been up-voted? A border must NEVER be drawn into the CLIENT area. A border is part of the NON-CLIENT area and if you draw it into the client area it will be inside the scrollbars and when scrolling you get crap on the screen!
-
Elmue over 7 yearsThis answer is WRONG. I don't undestand why I find wo many wrong answers on Stackoverflow that have been up-voted? A border must NEVER be drawn into the CLIENT area. A border is part of the NON-CLIENT area and if you draw it into the client area it will be inside the scrollbars and when scrolling you get crap on the screen!
-
Sinatr over 7 years@Elmue, that's called workaround. I agree it has drawbacks, e.g. it's necessary to take care about content not overlapping drawn border, yet it's very easy and for some it may be an acceptable solution. I guess answer was upvoted by those.
-
JPProgrammer over 7 yearsI don't know what you're on about @Elmue, but this solution worked perfectly well for me (and others too, apparently). The question is simply about how to change the border colour around a panel, nothing about non-client areas or scrollbars (btw are you trying to say all controls in the client area should never have a border around them??). If you can provide a better solution we're all ears, but I suspect you just like spouting criticism without having any better answers yourself.
-
Elmue over 7 yearsYour knowledge about Windows programming is insufficient. Whatever you do in a Paint event handler or in OnPaint() will only affect the client area of the control. But the scrollbars are part of the non-client area, and a Panel is derived from ScrollableControl! So if your control has scrollbars you will paint your border INSIDE the scrollbars which is surely the wrong solution. If you want to do it correctly it is far more complicated. You will have to handle WM_NCCALCSIZE to define the width of your border and WM_NCPAINT to draw your border. Search the MSDN for that and study it!
-
leoinlios over 5 yearsAnd if you want to control the thickness: private void panel1_Paint(object sender, PaintEventArgs e) { Color col = Color.DarkBlue; ButtonBorderStyle bbs = ButtonBorderStyle.Solid; int thickness = 4; ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle, col, thickness, bbs, col, thickness, bbs, col, thickness, bbs, col, thickness, bbs); }