set panel border thickness in c# winform
Asked Answered
R

3

7

I have searching and the result cannot solve my case. Actually I have a panel and I want the panel have thicker border than Windows given. I need BorderStyle

BorderStyle.FixedSingle

thicker.. Thanks before

Riesman answered 3/10, 2013 at 16:0 Comment(2)
Use use the Paint() event to make it look as thick as you want.Ironic
This is a duplicate. However, the accepted answer here is the only one that worked for me. The other solutions clipped off the right and bottom borders.Splenectomy
I
25

You have to customize your own Panel with a little custom painting:

//Paint event handler for your Panel
private void panel1_Paint(object sender, PaintEventArgs e){ 
  if(panel1.BorderStyle == BorderStyle.FixedSingle){
     int thickness = 3;//it's up to you
     int halfThickness = thickness/2;
     using(Pen p = new Pen(Color.Black,thickness)){
       e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
                                                 halfThickness,
                                                 panel1.ClientSize.Width-thickness,
                                                 panel1.ClientSize.Height-thickness));
     }
  }
}

Here is the screen shot of panel with thickness of 30:

Screen shot of panel with border thickness of 30

NOTE: The Size of Rectangle is calculated at the middle of the drawing line, suppose you draw line with thickness of 4, there will be an offset of 2 outside and 2 inside.

I didn't test the case given by Mr Hans, to fix it simply handle the event SizeChanged for your panel1 like this:

private void panel1_SizeChanged(object sender, EventArgs e){
   panel1.Invalidate();
}

You can also setting ResizeRedraw = true using Reflection without having to handle the SizeChanged event as above like this:

typeof(Control).GetProperty("ResizeRedraw", BindingFlags.NonPublic | BindingFlags.Instance)
               .SetValue(panel1, true, null);

You may see a little flicker when resizing, just add this code to enable doubleBuffered for your panel1:

typeof(Panel).GetProperty("DoubleBuffered",
                          BindingFlags.NonPublic | BindingFlags.Instance)
             .SetValue(panel1,true,null);
Iroquois answered 3/10, 2013 at 16:16 Comment(7)
Thanks, that's what actually I want :DRiesman
Dock the panel on the right+bottom and resize the form to see it go wrong.Ironic
@HansPassant thanks for pointing it out, looks like when customizing control we always have to add code handling resizing.Iroquois
@KingKing one more to my collection.Nanine
@HighCore what have you been collecting? Don't tell me that you have been collecting winforms sample code to learn it better :)))Iroquois
@KingKing I've been collecting samples that show how things that can be achieved with a single line of XAML require all sorts of horrible hacks in crappy dinosaur winforms =). This would be <Border BorderThickness="30"/>, y'knowNanine
@KingKing I've also discovered recently that winforms doesn't support changing the Font Size. I'm in awe.Nanine
T
5

To create a panel with border I place a panel in a panel. The "border panel" has the background color of the wanted border color and a padding, while the padding size is the wanted border thickness.

The advantage of this solution is that there is no flickering and no problems with resize.

enter image description here

enter image description here

This can be very simple be created in the designer or in code behind.

Code behind:

Panel panel_Border = new Panel();
Panel panel_Embedded = new Panel();

panel_Border.BackColor = Color.Green;
panel_Border.Controls.Add(panel_Embedded);
// this is the border thickness
panel_Border.Padding = new System.Windows.Forms.Padding(6);
panel_Border.Size = new System.Drawing.Size(200, 100);
        
panel_Embedded.BackColor = System.Drawing.SystemColors.Control;
panel_Embedded.Dock = System.Windows.Forms.DockStyle.Fill;
Task answered 1/7, 2020 at 6:6 Comment(0)
P
0

Create a new, slightly larger panel and set the background colour to Black (or whatever). Place the original panel INSIDE the larger panel.

Prestidigitation answered 8/11, 2017 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.