Validation in Windows Forms DateTimePicker (SfDateTimeEdit)
10 Jun 20213 minutes to read
The SfDateTimeEdit
control validates the DateTime value when the Enter key is pressed, so that the control lost its focus or the date is picked from the drop-down calendar.
Validation reset option
The ValidationOption that helps you to decide how the value will be changed when the validation is failed. If the validation is failed, the value will reset with previous date-time Value or MinValue
or MaxValue
. If the validating event is not handled then the validation will be done based on the ValidationOption
of the SfDateTimeEdit.
The validation results can be obtained based on the input provided to Value
or DateTimeText
of the SfDateTimeEdit. If the provided input has invalid date-time format or value that meets minimum or maximum value constraint, then the validation results will be failed.
-
Reset: A control that maintains the previous value before validating. If the validation is failed then the value will be reset with the previous value.
-
MinValue: Resets value with the
MinValue
, when the validation is failed. -
MaxValue: Resets value with the
MaxValue
, when the validation is failed.
Syncfusion.WinForms.Input.SfDateTimeEdit dateTimeEdit = new Syncfusion.WinForms.Input.SfDateTimeEdit();
this.Controls.Add(dateTimeEdit);
dateTimeEdit.Value = new DateTime(2018, 2, 1);
dateTimeEdit.DateTimeEditingMode = DateTimeEditingMode.Default;
// On Validation failed the value will be reset with MinValue.
dateTimeEdit.ValidationOption = ValidationResetOption.MinValue;
Dim dateTimeEdit As Syncfusion.WinForms.Input.SfDateTimeEdit = New Syncfusion.WinForms.Input.SfDateTimeEdit
Me.Controls.Add(dateTimeEdit)
dateTimeEdit.Value = New DateTime(2018, 2, 1)
dateTimeEdit.DateTimeEditingMode = DateTimeEditingMode.Default
' On Validation failed the value will be reset with MinValue.
dateTimeEdit.ValidationOption = ValidationResetOption.MinValue
The given value can be treated as a date/time value. It can be validated based on the DateTime format with culture. The following error indicating image will be shown, when the validation test is failed.
Handle validation
The ValidatingEventArgs
provides data for the Validating event of the SfDateTimeEdit control. By handling the Validating
event, it is possible to find the cause for validation failure with error message in the ValidatingEventArgs
.
-
IsError: Indicates whether the entered date and time is valid or invalid.
-
ErrorMessage: Updates the cause of the error. The error may be caused due to minimum or maximum value constraint met or incorrect date time format.
// Invoking Validating event
this.dateTimeEdit.Validating += DateTimeEdit_Validating;
private void DateTimeEdit_Validating(object sender, ValidatingEventArgs e)
{
if (e.IsError)
{
MessageBox.Show(e.ErrorMessage);
}
}
' Invoking Validating event
Me.dateTimeEdit.Validating = (Me.dateTimeEdit.Validating + DateTimeEdit_Validating)
Private Sub DateTimeEdit_Validating(ByVal sender As Object, ByVal e As ValidatingEventArgs)
If e.IsError Then
MessageBox.Show(e.ErrorMessage)
End If
End Sub