Category / Section
How to validate date selection in WinForms DateTimePicker (DateTimePickerAdv)?
1 min read
Validate the current date
Below illustrated two event handlers will prevent a date earlier than Today from being selected and make this visually obvious in the dropdown Calendar.
C#
private void Form1_Load(object sender, System.EventArgs e)
{
this.dateTimePickerAdv1.MinValue = DateTime.Today;
}
C#
private void dateTimePickerAdv1_Calendar_DateCellQueryInfo(object sender, Syncfusion.Windows.Forms.Tools.DateCellQueryInfoEventArgs e)
{
if (((DateTime)e.DateValue) < DateTime.Today)
{
e.Style.Enabled = false;
e.Style.Font.Strikeout = true;
e.Style.TextColor = Color.Gray;
}
}
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Me.dateTimePickerAdv1.MinValue = DateTime.Today End Sub
VB
Private Sub dateTimePickerAdv1_Calendar_DateCellQueryInfo(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Tools.DateCellQueryInfoEventArgs) If CType(e.DateValue, DateTime) < DateTime.Today Then e.Style.Enabled = False e.Style.Font.Strikeout = True e.Style.TextColor = Color.Gray End If End Sub