HeaderPane
Main
Hide
Share Link
Print

Validation

Values entered by the user are checked in three steps:

  1. The value must lie within the SftDate.MinDate and SftDate.MaxDate limits. A value outside them is moved to the nearest limit.
  2. The SftDate.ValueValidating event occurs. The event handler can refuse the value or change it.
  3. The value is accepted and the SftDate.ValueChanged event occurs (in range mode also SftDate.RangeChanged).

Values set by the application in code do not raise the ValueValidating event.

Refusing or Changing a Value

The ValueValidatingEventArgs.NewValue property holds the value the user entered, or null when the user empties the field; in range mode, ValueValidatingEventArgs.NewRangeEnd holds the last date of the range. Setting ValueValidatingEventArgs.Cancel refuses the value, and the field shows the previous value again. Changing NewValue or NewRangeEnd accepts a different value instead.

This example refuses Saturdays and Sundays:

C#

private void sftDate1_ValueValidating(object sender, ValueValidatingEventArgs e) {
    if (e.NewValue.HasValue && (e.NewValue.Value.DayOfWeek == DayOfWeek.Saturday || e.NewValue.Value.DayOfWeek == DayOfWeek.Sunday))
        e.Cancel = true;
}

VB

Private Sub SftDate1_ValueValidating(sender As Object, e As ValueValidatingEventArgs) Handles SftDate1.ValueValidating
    If e.NewValue.HasValue AndAlso (e.NewValue.Value.DayOfWeek = DayOfWeek.Saturday OrElse e.NewValue.Value.DayOfWeek = DayOfWeek.Sunday) Then
        e.Cancel = True
    End If
End Sub

Days That Cannot Be Selected

In a calendar, including the dropdown calendar of a field, days can be made unavailable: the CalendarClass.DisabledDaysOfWeek property disables days of the week, a special date with its SpecialDateClass.Enabled property cleared disables a day (see Special Dates), and the DayFormatting event can disable any day (see Day Formatting). Disabled days are shown in the CalendarClass.DisabledForeColor color and cannot be selected.

These settings apply to the calendar only. A field's ValueValidating event refuses such days when the user types them.

Text Parsing

A text the user types in a free-form field or pastes into a field can be parsed by the application in the SftDate.TextParsing event before the field parses it (see Editing).