views:

25

answers:

1

I would like to override System.Windows.Forms.UserControl to draw a custom border (e.g. using custom color). It's not possiblу to do usign built-in classes, because the only method/property you can affect the border behavior is BorderStyle.

Overriding OnPaint the following way (code below) is not a good solution, because it's basically drawing another border on top of original one.

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

        if (this.BorderStyle == BorderStyle.FixedSingle)
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.LightGray, ButtonBorderStyle.Solid);
    }

Does anyone know how to override border drawing in custom control?

Putting this user control into a panel is not an option in my case for certain reasons.

+1  A: 

Set base.BorderStyle to None to the default border isn't drawn. You'll need to override the BorderStyle property to make this work.

    public UserControl1() {
        InitializeComponent();
        base.BorderStyle = BorderStyle.None;
        this.BorderStyle = BorderStyle.FixedSingle;
    }

    private BorderStyle border;

    public new BorderStyle BorderStyle {
        get { return border; }
        set {
            border = value;
            Invalidate();
        }
    }
Hans Passant
Thank you, I came up with the similar solution. But using not BorderStyle type for a property, but just boolean value (whether draw or not).However, this doesn't solve the original problem: how not to redraw the border, but override built-in functionality to draw the border I need.
Nikita G.