HeaderPane
Main
Hide
Share Link
Print

Editing

The SftDate.EditStyle property defines how the user enters the value of a field: one part at a time (EditStyleEnum.Segments, the default) or by typing the whole value as text (EditStyleEnum.FreeForm).

Editing Parts

With EditStyleEnum.Segments, each part of the value (day, month, year, hours, minutes, seconds, AM/PM, era) is edited separately. The part being edited is highlighted in the SftDate.SegmentHighlightBackColor and SftDate.SegmentHighlightForeColor colors.

  • Digits type the number of a part. With the SftDate.AutoAdvance property set (the default), the next part is edited as soon as the number is complete; a separator character, such as "/" or ":", also moves to the next part.
  • Letters choose a month by its name and AM or PM.
  • The Up and Down keys, the + and - keys, the mouse wheel and the up and down buttons change the part being edited. Days, months and hours wrap around within their range.
  • The Left and Right keys, Tab and Shift+Tab move between the parts; Home and End move to the first and the last part.
  • Delete and Backspace empty the part being edited.
  • Ctrl+C copies the formatted value, Ctrl+V pastes a text and parses it like a typed text.

When a change creates a date that does not exist, such as February 30, the day is reduced to the last day of the month.

Free-Form Editing

With EditStyleEnum.FreeForm, the user types the value as text. The text is parsed when the user presses Enter, leaves the field or opens the dropdown. A text that cannot be parsed is rejected with a beep and the previous value is kept; an empty text empties the field when the SftDate.AllowEmpty or SftDate.ShowCheckBox property is set. The Up and Down keys accept the text and change the value by one day, or by one minute in a field showing only a time.

A text is parsed in the following order:

  1. The SftDate.TextParsing event, where the application can parse the text itself.
  2. The texts =Today, =Yesterday, =Tomorrow and =Now (see Values and Empty Fields).
  3. The field's format in its culture.
  4. Any date and time the culture understands (DateTime.TryParse).

Parts the format does not show are kept from the current value; a time typed in a date and time field keeps the date. In range mode, the first and the last date are separated by the SftDate.RangeSeparator text.

Parsing Text Yourself

The SftDate.TextParsing event occurs before a text typed in a free-form field or pasted into a field is parsed. The event handler sets TextParsingEventArgs.Handled to indicate that it parsed the text, and TextParsingEventArgs.Value to the value the text represents (or null if the text is invalid). This example accepts "+7" or "-3" as a number of days from the current value:

C#

private void sftDate1_TextParsing(object sender, TextParsingEventArgs e) {
    int days;
    if ((e.Text.StartsWith("+") || e.Text.StartsWith("-")) && int.TryParse(e.Text, out days)) {
        e.Value = e.CurrentValue.AddDays(days);
        e.Handled = true;
    }
}

VB

Private Sub SftDate1_TextParsing(sender As Object, e As TextParsingEventArgs) Handles SftDate1.TextParsing
    Dim days As Integer
    If (e.Text.StartsWith("+") OrElse e.Text.StartsWith("-")) AndAlso Integer.TryParse(e.Text, days) Then
        e.Value = e.CurrentValue.AddDays(days)
        e.Handled = True
    End If
End Sub

The value is then checked like any value the user enters (see Validation).