I'm using a DateTimePicker control in a Windows Form to get a time input from the user. I want to make the control borderless, but by default there's no way to turn off the border. I've overridden the OnPaint() in a control before, so I have been trying to do that here but I can't seem to get it to work. I have created a BorderlessDateTimePicker class that extends DateTimePicker, but I can't paint over the border. Perhaps someone could have a look at my code and tell me where I'm going wrong, or if there's anything else I need to do.
My code is as follows:
In my onPaint() method:
protected override void OnPaint(PaintEventArgs e)
{
GraphicsPath border = new GraphicsPath();
Pen borderPen = new Pen(Color.White);
// Set up path
border.AddLine(0, 0, this.ClientRectangle.Width, 0);
border.AddLine(this.ClientRectangle.Width, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
border.AddLine(this.ClientRectangle.Width, this.ClientRectangle.Height, 0, this.ClientRectangle.Height);
border.CloseFigure();
base.OnPaint(e);
// Draw path
e.Graphics.DrawPath(borderPen, border);
}
In my constructor:
public BorderlessDateTimePicker()
{
this.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Format = System.Windows.Forms.DateTimePickerFormat.Time;
this.ShowUpDown = true;
this.Size = new System.Drawing.Size(300, 75);
SetStyle(ControlStyles.ResizeRedraw, true);
}
Any suggestions?
Update 03/11/21:
I have tried a few things based on what other users have suggested. Code in the onPaint() now looks like this:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (GraphicsPath border = new GraphicsPath())
{
// Set up path
//border.AddLine(0, 0, this.ClientRectangle.Width, 0);
//border.AddLine(this.ClientRectangle.Width, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);
//border.AddLine(this.ClientRectangle.Width, this.ClientRectangle.Height, 0, this.ClientRectangle.Height);
//border.CloseFigure();
//Draw path
using (Pen borderPen = Pens.Red)
{
// Set border width to a ridiculously high value for testing
borderPen.Width = 10;
// Draw a line from top left corner to 20,20
border.AddLine(0, 0, 20, 20);
e.Graphics.DrawPath(borderPen, border);
}
}
}
It's still not working. But interestingly if I look in the Events list for my control, there's no Paint event listed - which would suggest to me that you cannot override onPaint for DateTimePicker, so maybe I'm barking up the wrong tree.