R – VisualStudio: How to add the dotted border to a UserControl at design time

user-controlsvisual studio

i have a user control that descends from UserControl.

When dropped onto the form the user control is invisible, because it doesn't have any kind of border to show itself.

The PictureBox and Panel controls, at design time, draws a dashed 1px border to make it visible.

What is the proper way to do that? Is there an attribute you can use to make VS add that?

Best Answer

There is no property that will do this automatically. However you can archive this by overriding the OnPaint in your control and manually drawing the rectangle.

Inside the overridden event you can call base.OnPaint(e) to draw the controls content and then add use the graphics object to paint the dotted line around the edge.

 protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (this.DesignMode)
             ControlPaint.DrawBorder( e.Graphics,this.ClientRectangle,Color.Gray,ButtonBorderStyle.Dashed);   
    }

As you can see you will need to wrap this extra code in an if statement that queries the controls DesignMode property so it only draws in your IDE.