I want the panel to have a tight border. Can i install this?

I want the panel to have a tight border. Can I install it somehow?

PS, I am using C #. VS 2008.

+4
source share
5 answers

Jim

I made a custom control and it is provided with a ParentControlDesigner. As I pointed out in my comment, this is not an ideal solution for what you are asking. But this should be a good starting point. Oh, any FYI, I have this with a custom border color. I was inspired by yet another SO post to pursue this ... It was harder than I expected. To configure the parameters correctly, when performing border size adjustment, a PerformLayout call is made. Overriding to DisplayRectangle and calling SetDisplayRectLocation in OnResize causes the child controls to move correctly. In addition, the child controls do not have the expected “0,0” if in the upper left corner ... if the border width is not set to 0 ... And OnPaint provides a custom border drawing.

Good luck to you! Creating custom controls that are parents is difficult, but not impossible.

[Designer(typeof(ParentControlDesigner))] public partial class CustomPanel : UserControl { Color _borderColor = Color.Blue; int _borderWidth = 5; public int BorderWidth { get { return _borderWidth; } set { _borderWidth = value; Invalidate(); PerformLayout(); } } public CustomPanel() { InitializeComponent(); } public override Rectangle DisplayRectangle { get { return new Rectangle(_borderWidth, _borderWidth, Bounds.Width - _borderWidth * 2, Bounds.Height - _borderWidth * 2); } } public Color BorderColor { get { return _borderColor; } set { _borderColor = value; Invalidate(); } } new public BorderStyle BorderStyle { get { return _borderWidth == 0 ? BorderStyle.None : BorderStyle.FixedSingle; } set { } } protected override void OnPaint(PaintEventArgs e) { base.OnPaintBackground(e); if (this.BorderStyle == BorderStyle.FixedSingle) { using (Pen p = new Pen(_borderColor, _borderWidth)) { Rectangle r = ClientRectangle; // now for the funky stuff... // to get the rectangle drawn correctly, we actually need to // adjust the rectangle as .net centers the line, based on width, // on the provided rectangle. r.Inflate(-Convert.ToInt32(_borderWidth / 2.0 + .5), -Convert.ToInt32(_borderWidth / 2.0 + .5)); e.Graphics.DrawRectangle(p, r); } } } protected override void OnResize(EventArgs e) { base.OnResize(e); SetDisplayRectLocation(_borderWidth, _borderWidth); } } 
+9
source

Just execute the Paint Paint event and draw a border. For instance:

 using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); panel1.Paint += panel1_Paint; } VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Button.PushButton.Normal); private void panel1_Paint(object sender, PaintEventArgs e) { renderer.DrawEdge(e.Graphics, panel1.ClientRectangle, Edges.Bottom | Edges.Left | Edges.Right | Edges.Top, EdgeStyle.Raised, EdgeEffects.Flat); } } } 

Play with arguments to find something you like. You must add code to return to ControlPaint.DrawBorder if visual styles are not included. Fur.

+3
source

If this is just a presentation, place a panel that fills the form with the background color of the border color you want and the Dock style for Fill. Place another panel inside this with the default background color and Dock style for Fill. Play with the padding and margins of the two panels to get the border size you want (I forgot which parameter applies to the inner panel and the outer panel). Place the controls on the inner panel. If both panels are set to Dock = Fill, formal resizing is automatically processed for you. You may have to experiment with some controls, but I have done this many times without problems for both the main application windows and pop-up forms.

+3
source

This is an old post, but I still find it useful. And I found another way .

 ControlPaint.DrawBorder(e.Graphics, control.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); 
+3
source

This is a kind of snap, but I always used a shortcut for each side border. You will need to set the autosize property to false and the dock on one side (left, right, top, bottom). Then just set the color width / height / background to do what you want.

You can easily make this a custom control and just set some custom public properties to set the width / height and background color of all the shortcuts for you to change the color.

0
source

All Articles