This sample shows labeled date fields of every kind and a month calendar, with options that change their culture and calendar system, dark mode, high contrast, buttons, border, dropdown alignment, months, week numbers, first day of the week, views, Today and Clear buttons, special dates, the DayFormatting event, disabled weekends, range selection, limits, colors and themes.
The source code is located at C:\Program Files (x86)\Softelvdm\SftDate OCX 2.0\Samples\Visual Studio - VB.NET\DemoSample\Form1.vb.
' A culture is a locale name (empty for the Windows setting) and a calendar system (the locale's own, or one of its alternate calendars)
Private Class Culture
Public Name, Locale As String
Public Calendar As SftDateCalendarTypeConstants
Public Sub New(name As String, locale As String, calendar As SftDateCalendarTypeConstants)
Me.Name = name
Me.Locale = locale
Me.Calendar = calendar
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Private Shared ReadOnly cultures As Culture() = {
New Culture("(Windows setting)", "", SftDateCalendarTypeConstants.calendarSftDateDefault),
New Culture("English (United States)", "en-US", SftDateCalendarTypeConstants.calendarSftDateDefault),
New Culture("German (Germany)", "de-DE", SftDateCalendarTypeConstants.calendarSftDateDefault),
New Culture("French (France)", "fr-FR", SftDateCalendarTypeConstants.calendarSftDateDefault),
New Culture("Japanese (Japan), Japanese calendar", "ja-JP", SftDateCalendarTypeConstants.calendarSftDateJapanese),
New Culture("Arabic (Saudi Arabia), Um Al Qura calendar", "ar-SA", SftDateCalendarTypeConstants.calendarSftDateDefault),
New Culture("Hebrew (Israel), Hebrew calendar", "he-IL", SftDateCalendarTypeConstants.calendarSftDateHebrew),
New Culture("Thai (Thailand), Buddhist calendar", "th-TH", SftDateCalendarTypeConstants.calendarSftDateDefault),
New Culture("Persian (Iran), Persian calendar", "fa-IR", SftDateCalendarTypeConstants.calendarSftDateDefault)
}
Private Shared ReadOnly borderStyles As SftDateBorderConstants() = {
SftDateBorderConstants.borderSftDateFixedSingle, SftDateBorderConstants.borderSftDateFixed3D, SftDateBorderConstants.borderSftDateNone
}
Private Shared ReadOnly viewSets As SftDateViewsConstants() = {
SftDateViewsConstants.viewsSftDateAll,
SftDateViewsConstants.viewsSftDateMonth Or SftDateViewsConstants.viewsSftDateYear,
SftDateViewsConstants.viewsSftDateMonth
}
Private Const DefaultColor As UInteger = CUInt(SftDateColorConstants.colorSftDateDefault And &HFFFFFFFFL)
Private Shared Function Ole(color As Color) As UInteger
Return CUInt(ColorTranslator.ToOle(color))
End Function
Private fields As AxSftDate()
Private minDate, maxDate As Date ' the controls' own limits
Private initialized As Boolean
' calls field for each field and calendar for the calendar
Private Sub ForEachControl(field As Action(Of AxSftDate), calendar As Action(Of AxSftCalendar))
For Each f As AxSftDate In fields
field(f)
Next
calendar(sftCalendar)
End Sub
' calls action for the Calendar object of each control, which holds the calendar settings of the fields' dropdowns too
Private Sub ForEachCalendar(action As Action(Of SftDateCalendar))
ForEachControl(Sub(f) action(f.Calendar), Sub(c) action(c.Calendar))
End Sub
' ActiveX controls (AxHost) do not take part in the form's automatic scaling, so the form scales their positions and widths
Protected Overrides Sub ScaleControl(factor As SizeF, specified As BoundsSpecified)
MyBase.ScaleControl(factor, specified)
For Each c As Control In Controls
If TypeOf c Is AxHost Then c.SetBounds(CInt(Math.Round(c.Left * factor.Width)), CInt(Math.Round(c.Top * factor.Height)), CInt(Math.Round(c.Width * factor.Width)), c.Height)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
fields = New AxSftDate() {
fieldDate, fieldLongDate, fieldTimeList, fieldTimeClock, fieldDateTime,
fieldRange, fieldOptional, fieldEmpty, fieldFreeForm, fieldCustom
}
' the fields' formats and buttons were set at design time; these follow Windows
Dim current As Date = Date.Now
ForEachControl(Sub(f)
f.DarkMode = SftDateDarkModeConstants.darkSftDateAuto
f.HighContrastMode = SftDateHighContrastConstants.highContrastSftDateAuto
f.ImageScaling = SftDateScalingConstants.scalingSftDateStretch
If f IsNot fieldEmpty AndAlso f IsNot fieldRange Then f.Value = current
End Sub,
Sub(c)
c.DarkMode = SftDateDarkModeConstants.darkSftDateAuto
c.HighContrastMode = SftDateHighContrastConstants.highContrastSftDateAuto
c.ImageScaling = SftDateScalingConstants.scalingSftDateStretch
c.Value = current
End Sub)
fieldRange.SetRange(current, current.AddDays(6))
minDate = sftCalendar.MinDate
maxDate = sftCalendar.MaxDate
labelHelp.Text = "Type into the parts of a field, change a part with the Up and Down keys or the mouse wheel, and open the " &
"dropdown with F4 or Alt+Down. In the dropdown of the date and time field, Tab switches between the calendar " &
"and the time. The free-form field parses what you type in the selected culture." & vbCrLf & vbCrLf &
"The options apply to all fields and calendars; the month counts apply to the SftCalendar control."
' the options start out with the controls' settings
Dim cal As SftDateCalendar = sftCalendar.Calendar
comboCulture.Items.AddRange(cultures)
comboCulture.SelectedIndex = 0
comboDarkMode.Items.AddRange(New Object() {"Off", "On", "Auto"})
comboDarkMode.SelectedIndex = CInt(sftCalendar.DarkMode)
comboHighContrast.Items.AddRange(New Object() {"Off", "On", "Auto"})
comboHighContrast.SelectedIndex = CInt(sftCalendar.HighContrastMode)
comboButtons.Items.AddRange(New Object() {"Dropdown", "Up/down", "Dropdown, up/down", "None"})
comboButtons.SelectedIndex = CInt(fieldDate.Buttons)
comboBorder.Items.AddRange(New Object() {"Single", "3D", "None"})
comboBorder.SelectedIndex = Math.Max(0, Array.IndexOf(borderStyles, fieldDate.BorderStyle))
comboAlignment.Items.AddRange(New Object() {"Near", "Far"})
comboAlignment.SelectedIndex = CInt(fieldDate.DropDownAlignment)
comboAcross.Items.AddRange(New Object() {"1", "2", "3"})
comboAcross.SelectedIndex = cal.MonthsAcross - 1
comboDown.Items.AddRange(New Object() {"1", "2"})
comboDown.SelectedIndex = cal.MonthsDown - 1
comboWeekNumbers.Items.AddRange(New Object() {"None", "Culture rule", "ISO 8601"})
comboWeekNumbers.SelectedIndex = If(Not cal.ShowWeekNumbers, 0, If(cal.WeekNumberRule = SftDateWeekRuleConstants.weekRuleSftDateISO8601, 2, 1))
comboFirstDay.Items.AddRange(New Object() {"(Culture)", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"})
comboFirstDay.SelectedIndex = CInt(cal.FirstDayOfWeek)
comboViews.Items.AddRange(New Object() {"All", "Month and year", "Month only"})
comboViews.SelectedIndex = Math.Max(0, Array.IndexOf(viewSets, cal.AllowedViews))
checkTrailing.Checked = cal.ShowTrailingDates
checkTodayButton.Checked = cal.ShowTodayButton
checkHighlightToday.Checked = cal.HighlightToday
checkClearButton.Checked = cal.ShowClearButton
checkDropOnFocus.Checked = fieldDate.DropOnFocus
checkAutoAdvance.Checked = fieldDate.AutoAdvance
checkThemes.Checked = sftCalendar.UseThemes
initialized = True
ShowSelection()
DoLayout()
End Sub
' The calendar sizes itself to its months, so its group box, the selection text, the Close button and the form follow it.
Private Sub DoLayout()
Dim margin As Integer = groupFields.Left
sftCalendar.Location = New Point(groupCalendar.Left + margin, groupCalendar.Top + 2 * margin)
Dim width As Integer = Math.Max(groupOptions.Width, sftCalendar.Width + 2 * margin)
labelSelection.SetBounds(sftCalendar.Left, sftCalendar.Bottom + margin \ 2, width - 2 * margin, labelSelection.Height)
groupCalendar.Size = New Size(width, labelSelection.Bottom + margin - groupCalendar.Top)
Dim right As Integer = groupCalendar.Right + margin
Dim bottom As Integer = Math.Max(groupCalendar.Bottom, labelHelp.Bottom) + margin
buttonClose.Location = New Point(right - margin - buttonClose.Width, bottom)
ClientSize = New Size(right, buttonClose.Bottom + margin)
End Sub
Private Function FormatLongDate(value As Date) As String
Dim culture As Culture = cultures(comboCulture.SelectedIndex)
Dim info As CultureInfo = CType(If(culture.Locale.Length = 0, CultureInfo.CurrentCulture, New CultureInfo(culture.Locale)).Clone(), CultureInfo)
If culture.Calendar = SftDateCalendarTypeConstants.calendarSftDateJapanese Then
info.DateTimeFormat.Calendar = New JapaneseCalendar()
ElseIf culture.Calendar = SftDateCalendarTypeConstants.calendarSftDateHebrew Then
info.DateTimeFormat.Calendar = New HebrewCalendar()
End If
Return value.ToString("D", info)
End Function
Private Sub ShowSelection()
Dim selection As String = "Selected: (none)"
If sftCalendar.SelectionMode = SftDateSelectionModeConstants.selectionSftDateRange Then
If TypeOf sftCalendar.RangeStart Is Date Then
Dim start As Date = CDate(sftCalendar.RangeStart)
Dim [end] As Date = If(TypeOf sftCalendar.RangeEnd Is Date, CDate(sftCalendar.RangeEnd), start)
selection = "Selected: " & FormatLongDate(start) & " - " & FormatLongDate([end])
End If
ElseIf TypeOf sftCalendar.Value Is Date Then
selection = "Selected: " & FormatLongDate(CDate(sftCalendar.Value))
End If
labelSelection.Text = selection
End Sub
Private Sub sftCalendar_ValueChanged(sender As Object, e As EventArgs) Handles sftCalendar.ValueChanged, sftCalendar.RangeChanged
ShowSelection()
End Sub
' Colors set by the application are used as given in dark mode too, so the scheme has a variant for each mode.
Private Sub SetColorScheme(cal As SftDateCalendar, dark As Boolean, [on] As Boolean)
cal.TitleBackColor = If([on], Ole(Color.FromArgb(0, 95, 115)), DefaultColor)
cal.TitleForeColor = If([on], Ole(Color.White), DefaultColor)
cal.DayOfWeekForeColor = If(Not [on], DefaultColor, If(dark, Ole(Color.FromArgb(110, 190, 205)), Ole(Color.FromArgb(0, 95, 115))))
cal.WeekNumberForeColor = If(Not [on], DefaultColor, If(dark, Ole(Color.FromArgb(110, 190, 205)), Ole(Color.FromArgb(0, 95, 115))))
cal.TodayColor = If([on], Ole(Color.FromArgb(225, 140, 30)), DefaultColor)
cal.SelectedBackColor = If([on], Ole(Color.FromArgb(225, 140, 30)), DefaultColor)
cal.SelectedForeColor = If([on], Ole(Color.White), DefaultColor)
cal.RangeBackColor = If(Not [on], DefaultColor, If(dark, Ole(Color.FromArgb(95, 60, 20)), Ole(Color.FromArgb(250, 225, 190))))
End Sub
Private Sub SetColorScheme(field As AxSftDate, [on] As Boolean)
SetColorScheme(field.Calendar, field.DarkModeActive, [on])
Dim ocx As ISftDate = CType(field.GetOcx(), ISftDate)
ocx.SegmentHighlightBackColor = If([on], Ole(Color.FromArgb(225, 140, 30)), DefaultColor)
ocx.SegmentHighlightForeColor = If([on], Ole(Color.White), DefaultColor)
End Sub
Private Sub SetColorSchemes()
ForEachControl(Sub(f) SetColorScheme(f, checkColors.Checked), Sub(c) SetColorScheme(c.Calendar, c.DarkModeActive, checkColors.Checked))
End Sub
Private Sub field_DarkModeChange(sender As Object, e As _ISftDateEvents_DarkModeChangeEvent) Handles fieldDate.DarkModeChange, fieldLongDate.DarkModeChange,
fieldTimeList.DarkModeChange, fieldTimeClock.DarkModeChange, fieldDateTime.DarkModeChange, fieldRange.DarkModeChange,
fieldOptional.DarkModeChange, fieldEmpty.DarkModeChange, fieldFreeForm.DarkModeChange, fieldCustom.DarkModeChange
SetColorScheme(CType(sender, AxSftDate), checkColors.Checked)
End Sub
Private Sub sftCalendar_DarkModeChange(sender As Object, e As _ISftCalendarEvents_DarkModeChangeEvent) Handles sftCalendar.DarkModeChange
SetColorScheme(sftCalendar.Calendar, e.active, checkColors.Checked)
End Sub
' Special dates repeat every year, month or week, and can be bold, colored, disabled, show a picture and a tooltip.
Private Sub SetHolidays(show As Boolean)
Dim today As Date = Date.Today
Dim monday As Date = today.AddDays(-((CInt(today.DayOfWeek) + 6) Mod 7))
ForEachCalendar(Sub(cal)
Dim dates As SftDateSpecialDates = cal.SpecialDates
dates.Clear()
If Not show Then Return
Dim day As SftDateSpecialDate = dates.Add(New Date(today.Year, 1, 1), SftDateRecurrenceConstants.recurrenceSftDateAnnually)
day.Bold = True
day.ForeColor = Ole(Color.Firebrick)
day.ToolTip = "New Year's Day"
day = dates.Add(New Date(today.Year, 7, 4), SftDateRecurrenceConstants.recurrenceSftDateAnnually)
day.Bold = True
day.ForeColor = Ole(Color.Firebrick)
day.ToolTip = "Independence Day"
day = dates.Add(New Date(today.Year, 12, 25), SftDateRecurrenceConstants.recurrenceSftDateAnnually)
day.Bold = True
day.ForeColor = Ole(Color.Firebrick)
day.BackColor = Ole(Color.MistyRose)
day.ToolTip = "Christmas Day"
day = dates.Add(New Date(today.Year, 1, 15), SftDateRecurrenceConstants.recurrenceSftDateMonthly)
day.Enabled = False
day.ToolTip = "Closed for inventory"
day = dates.Add(monday, SftDateRecurrenceConstants.recurrenceSftDateWeekly)
day.ForeColor = Ole(Color.RoyalBlue)
day.Picture.SetColorSample(Ole(Color.RoyalBlue), Ole(Color.RoyalBlue))
day.ToolTip = "Team meeting"
End Sub)
End Sub
' any day of any calendar can be changed here; this colors weekends that aren't special dates
Private Sub FormatDay(day As SftDateDayFormatting)
If checkWeekends.Checked AndAlso day.Weekend AndAlso day.SpecialDate < 0 Then day.ForeColor = Ole(Color.IndianRed)
End Sub
Private Sub field_DayFormatting(sender As Object, e As _ISftDateEvents_DayFormattingEvent) Handles fieldDate.DayFormatting, fieldLongDate.DayFormatting,
fieldTimeList.DayFormatting, fieldTimeClock.DayFormatting, fieldDateTime.DayFormatting, fieldRange.DayFormatting,
fieldOptional.DayFormatting, fieldEmpty.DayFormatting, fieldFreeForm.DayFormatting, fieldCustom.DayFormatting
FormatDay(e.day)
End Sub
Private Sub sftCalendar_DayFormatting(sender As Object, e As _ISftCalendarEvents_DayFormattingEvent) Handles sftCalendar.DayFormatting
FormatDay(e.day)
End Sub
Private Sub option_Changed(sender As Object, e As EventArgs) Handles comboCulture.SelectedIndexChanged, comboDarkMode.SelectedIndexChanged,
comboHighContrast.SelectedIndexChanged, comboButtons.SelectedIndexChanged, comboBorder.SelectedIndexChanged,
comboAlignment.SelectedIndexChanged, comboAcross.SelectedIndexChanged, comboDown.SelectedIndexChanged,
comboWeekNumbers.SelectedIndexChanged, comboFirstDay.SelectedIndexChanged, comboViews.SelectedIndexChanged,
checkTrailing.CheckedChanged, checkTodayButton.CheckedChanged, checkHighlightToday.CheckedChanged, checkClearButton.CheckedChanged,
checkHolidays.CheckedChanged, checkWeekends.CheckedChanged, checkDisableWeekends.CheckedChanged, checkRangeSelect.CheckedChanged,
checkThisYear.CheckedChanged, checkDropOnFocus.CheckedChanged, checkAutoAdvance.CheckedChanged, checkColors.CheckedChanged,
checkThemes.CheckedChanged
' Handles clauses also run while InitializeComponent sets up the form, before Form1_Load has set up the fields
If Not initialized Then Return
If sender Is comboCulture Then
Dim culture As Culture = cultures(comboCulture.SelectedIndex)
Dim rtl As Boolean = If(culture.Locale.Length = 0, CultureInfo.CurrentCulture, New CultureInfo(culture.Locale)).TextInfo.IsRightToLeft
' the new culture may not offer the current calendar system, so it is reset first
ForEachControl(Sub(f)
f.CalendarType = SftDateCalendarTypeConstants.calendarSftDateDefault
f.Culture = culture.Locale
f.CalendarType = culture.Calendar
f.CtlRightToLeft = rtl
End Sub,
Sub(c)
c.CalendarType = SftDateCalendarTypeConstants.calendarSftDateDefault
c.Culture = culture.Locale
c.CalendarType = culture.Calendar
c.CtlRightToLeft = rtl
End Sub)
ShowSelection()
ElseIf sender Is comboDarkMode Then
Dim mode As SftDateDarkModeConstants = CType(comboDarkMode.SelectedIndex, SftDateDarkModeConstants)
ForEachControl(Sub(f) f.DarkMode = mode, Sub(c) c.DarkMode = mode)
SetColorSchemes()
ElseIf sender Is comboHighContrast Then
Dim mode As SftDateHighContrastConstants = CType(comboHighContrast.SelectedIndex, SftDateHighContrastConstants)
ForEachControl(Sub(f) f.HighContrastMode = mode, Sub(c) c.HighContrastMode = mode)
ElseIf sender Is comboButtons Then
For Each f As AxSftDate In fields
f.Buttons = CType(comboButtons.SelectedIndex, SftDateButtonsConstants)
Next
ElseIf sender Is comboBorder Then
For Each f As AxSftDate In fields
f.BorderStyle = borderStyles(comboBorder.SelectedIndex)
Next
ElseIf sender Is comboAlignment Then
For Each f As AxSftDate In fields
f.DropDownAlignment = CType(comboAlignment.SelectedIndex, SftDateAlignConstants)
Next
ElseIf sender Is comboAcross Then
sftCalendar.Calendar.MonthsAcross = comboAcross.SelectedIndex + 1
ElseIf sender Is comboDown Then
sftCalendar.Calendar.MonthsDown = comboDown.SelectedIndex + 1
ElseIf sender Is comboWeekNumbers Then
ForEachCalendar(Sub(cal)
cal.ShowWeekNumbers = comboWeekNumbers.SelectedIndex > 0
cal.WeekNumberRule = If(comboWeekNumbers.SelectedIndex = 2, SftDateWeekRuleConstants.weekRuleSftDateISO8601, SftDateWeekRuleConstants.weekRuleSftDateCulture)
End Sub)
ElseIf sender Is comboFirstDay Then
ForEachCalendar(Sub(cal) cal.FirstDayOfWeek = CType(comboFirstDay.SelectedIndex, SftDateFirstDayConstants))
ElseIf sender Is comboViews Then
ForEachCalendar(Sub(cal) cal.AllowedViews = viewSets(comboViews.SelectedIndex))
ElseIf sender Is checkTrailing Then
ForEachCalendar(Sub(cal) cal.ShowTrailingDates = checkTrailing.Checked)
ElseIf sender Is checkTodayButton Then
ForEachCalendar(Sub(cal) cal.ShowTodayButton = checkTodayButton.Checked)
ElseIf sender Is checkHighlightToday Then
ForEachCalendar(Sub(cal) cal.HighlightToday = checkHighlightToday.Checked)
ElseIf sender Is checkClearButton Then
ForEachCalendar(Sub(cal) cal.ShowClearButton = checkClearButton.Checked)
ElseIf sender Is checkHolidays Then
SetHolidays(checkHolidays.Checked)
ElseIf sender Is checkWeekends Then
' the DayFormatting event colors the days; RefreshDays asks for them again
ForEachCalendar(Sub(cal) cal.RefreshDays())
ElseIf sender Is checkDisableWeekends Then
ForEachCalendar(Sub(cal) cal.DisabledDaysOfWeek = If(checkDisableWeekends.Checked, SftDateDaysConstants.daysSftDateWeekend, SftDateDaysConstants.daysSftDateNone))
ElseIf sender Is checkRangeSelect Then
sftCalendar.SelectionMode = If(checkRangeSelect.Checked, SftDateSelectionModeConstants.selectionSftDateRange, SftDateSelectionModeConstants.selectionSftDateSingle)
ShowSelection()
ElseIf sender Is checkThisYear Then
Dim first As New Date(Date.Today.Year, 1, 1), last As New Date(Date.Today.Year, 12, 31)
ForEachControl(Sub(f)
f.MinDate = If(checkThisYear.Checked, first, minDate)
f.MaxDate = If(checkThisYear.Checked, last, maxDate)
End Sub,
Sub(c)
c.MinDate = If(checkThisYear.Checked, first, minDate)
c.MaxDate = If(checkThisYear.Checked, last, maxDate)
End Sub)
ShowSelection()
ElseIf sender Is checkDropOnFocus Then
For Each f As AxSftDate In fields
f.DropOnFocus = checkDropOnFocus.Checked
Next
ElseIf sender Is checkAutoAdvance Then
For Each f As AxSftDate In fields
f.AutoAdvance = checkAutoAdvance.Checked
Next
ElseIf sender Is checkColors Then
SetColorSchemes()
ElseIf sender Is checkThemes Then
ForEachControl(Sub(f) f.UseThemes = checkThemes.Checked, Sub(c) c.UseThemes = checkThemes.Checked)
End If
DoLayout()
End Sub
Private Sub buttonClose_Click(sender As Object, e As EventArgs) Handles buttonClose.Click
Close()
End Sub
End Class