Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Jim,</p> <p>I've made a user control and given is a ParentControlDesigner. As I indicated in my comment it's not a perfect solution to what you're asking for. But it should be a good starting point. Oh any FYI, I've got it with a customizable border color too. I was inspired by another SO post to pursue this... It was trickier than I expected. To get things to rearrange correctly when setting the border size a call to PerformLayout is made. The override to DisplayRectangle and the call to SetDisplayRectLocation in OnResize cause the proper repositioning of the child controls. As well the child controls don't have the expected "0,0" when in the upper left most... unless border width is set to 0... And OnPaint provides the custom drawing of the border.</p> <p>Best of luck to ya! Making custom controls that are parents is tricky, but not impossible.</p> <pre><code>[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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload