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.
Step 2:- Create an example form and check the validation onMouseUp
event and button1_Click
events
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.