Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Panel Border Color

In the properties of a Panel I have set the border style to Fixed 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:

Screenshot of actual result

but I want a fixed single border like this:

Screenshot of desited result

How I make the border in yellow?

like image 783
yogeshkmrsoni002 Avatar asked Jan 08 '14 12:01

yogeshkmrsoni002


2 Answers

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); } 
like image 179
Raihan Al-Mamun Avatar answered Sep 17 '22 14:09

Raihan Al-Mamun


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);
    }

}
like image 20
Sinatr Avatar answered Sep 19 '22 14:09

Sinatr