[Solved] Changing the border color of a DateTimePicker validation .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.




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.

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.

Post a Comment

Previous Post Next Post