[Solved] Changing the border color of a DateTimePicker validation in .NET in C# Windows forms

Problem

In Windows Forms, when border color is not changed after setting the border color using the property of a DateTimePicker (Date Time Picker). Then how to change or set the border color to red in case of error validation when the field is empty and showCheckbox property is true and the checkbox is unchecked?

Solution

As you know, there is no straightforward answer to this problem, but we can create some alternative to meet the requirement. So if the user unchecks the DTP checkbox (showCheckbox property) border would be red in color with an error message box.

If you just want to change the border color of a DateTimePicker, you can use a Custom Control derived DateTimePicker, override its WndProc, trap WM_PAINT and draw a border with a color of choice.

Step 1; – Create Custom Datetimepicker CustomDTP.cs class and set customFormat property to CustomDTP as shown in the below image.

using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
     class CustomDTP : DateTimePicker
     {
         [DllImport("user32.dll", EntryPoint = "sendMessageA")]
         private static extern int SendMessege(IntPtr hwnd, int wMsg, IntPtr wParam, object lParam);
     [DllImport("user32")]
    private static extern IntPtr GetWindowDC(IntPtr hwnd);

    [DllImport("user32")]
    private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    const int WM_PAINT = 0xF;
    const int WM_NC_PAINT = 0x85;
    public Color BorderColor { get; set; }
    protected override void WndProc(ref Message m)
    {
        IntPtr hDC = IntPtr.Zero;
        Graphics gdc = null;
        switch (m.Msg)
        {
            case WM_NC_PAINT:
                hDC = GetWindowDC(m.HWnd);
                gdc = Graphics.FromHdc(hDC);
                ControlBorder(gdc, BorderColor);
                m.Result = (IntPtr)1;
                ReleaseDC(m.HWnd, hDC);
                gdc.Dispose();
                break;

            case WM_PAINT:
                base.WndProc(ref m);
                hDC = GetWindowDC(m.HWnd);
                gdc = Graphics.FromHdc(hDC);
                ControlBorder(gdc, BorderColor);
                ReleaseDC(m.HWnd, hDC);
                gdc.Dispose();
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }

    private void ControlBorder(Graphics gdc, Color borderColor)
    {
        gdc.DrawRectangle(new Pen(borderColor, 2), new Rectangle(0, 0, this.Width, this.Height));
    }
}

Step 2:- Create an example form and check the validation onMouseUp event and button1_Click events

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.Checked = false;
            dateTimePicker1.CustomFormat = " ";
            dateTimePicker1.BorderColor = Color.Gray;
        }

        private void dateTimePicker1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!dateTimePicker1.Checked)
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.Checked = false;
                dateTimePicker1.CustomFormat = " ";
                dateTimePicker1.BorderColor = Color.Red;
                MessageBox.Show("Enter Valid Date", "Form1", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.Checked = true;
                dateTimePicker1.BorderColor = Color.Gray;
                dateTimePicker1.CustomFormat = "MM-dd-yyyy";
            }
        }
        private void button1_Click(object sender, System.EventArgs e)
        {

            if (!dateTimePicker1.Checked)
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.Checked = false;
                dateTimePicker1.CustomFormat = " ";
                dateTimePicker1.BorderColor = Color.Red;
                MessageBox.Show("Enter Valid Date", "Form1", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                dateTimePicker1.Format = DateTimePickerFormat.Custom;
                dateTimePicker1.Checked = true;
                dateTimePicker1.CustomFormat = "MM-dd-yyyy";
                dateTimePicker1.BorderColor = Color.Gray;
                MessageBox.Show("Data Saved successsfully");
            }
        }
    }
}

Conclusion

In Windows Forms, using custom user control we can achieve complex requirements as we could achieve this particular requirement to highlight date picker border with red color on validation error. So custom user control is one of the key features of the Windows Forms application for customized user-friendly application development.

Default image
Shivaraju M

Over 3+ years experience in IT industry, good knowledge in . Net-based Web applications, windows applications, web services, and SPAs. I have played
multiple roles related to technical delivery in multiple domains while working on over 10+ technologies. I am
currently focused on continuous improvement and agile project management.

Leave a Reply