Reputation: 7493
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.
Upvotes: 5
Views: 10819
Reputation: 941465
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();
}
}
Upvotes: 7