0

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.

Student228
  • 271
  • 1
  • 4
  • 14
  • @HansPassant, that worked, but now the control is blank (i.e. I can't see the time!). – Student228 Oct 31 '21 at 13:44
  • You may want to set the pen width. Side point: Make sure you dispose your graphics objects such as `border` and `borderPen` with `using`. You may want to just use the standard `Pens.White` for efficiency – Charlieface Oct 31 '21 at 16:19
  • What I wonder about is that you call the base class after you drew the border. I would have expected this to happen before else the base class is overwriting your lines again? – Aldert Oct 31 '21 at 19:59
  • https://stackoverflow.com/a/66092509/14171304 – dr.null Oct 31 '21 at 22:31

0 Answers0