How to detect if my custom control in the editor or not?

237 views Asked by At

I've got a UserControl which I created myself, it is for display like PictureBox, but it displays my datas about something. So if no target thing to display things about it, it hasn't got any image to show. If this happens (always when I put my custom control on the form), the control can't be seen (only if I select it) because of no image to show. I want to do something like what the PictureBox does, in the editor when it hasn't got image to disaplay, it has a line border to "tell us" "here is a picture box on the form", but when I run the program and the PictureBox with no image, it hasn't got that border. Image. How can I detect this for my custom control?

1

There are 1 answers

0
King King On BEST ANSWER

You may use the property DesignMode to determine if your control is in design mode to draw the Rectangle around it, otherwise at runtime, the rectangle will not be drawn.

public class CustomControl : UserControl
{        
    protected override void OnPaint(PaintEventArgs e)
    {
        if (DesignMode){
            e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0,0,ClientSize.Width-1, ClientSize.Height-1));
        }
        base.OnPaint(e);
    }
}