HeaderPane
Main
Hide
Share Link
Print

DemoSample Sample (C#)

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 - CSharp\DemoSample\Form1.cs.


        // 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 string Name, Locale;
            public SftDateCalendarTypeConstants Calendar;
            public Culture(string name, string locale, SftDateCalendarTypeConstants calendar) { Name = name; Locale = locale; Calendar = calendar; }
            public override string ToString() { return Name; }
        }

        private static readonly Culture[] cultures = {
            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 static readonly SftDateBorderConstants[] borderStyles = {
            SftDateBorderConstants.borderSftDateFixedSingle, SftDateBorderConstants.borderSftDateFixed3D, SftDateBorderConstants.borderSftDateNone,
        };
        private static readonly SftDateViewsConstants[] viewSets = {
            SftDateViewsConstants.viewsSftDateAll,
            SftDateViewsConstants.viewsSftDateMonth | SftDateViewsConstants.viewsSftDateYear,
            SftDateViewsConstants.viewsSftDateMonth,
        };

        private const uint DefaultColor = unchecked((uint)SftDateColorConstants.colorSftDateDefault);
        private static uint Ole(Color color) { return (uint)ColorTranslator.ToOle(color); }

        private AxSftDate[] fields;
        private DateTime minDate, maxDate;          // the controls' own limits
        private bool initialized;

        // calls field for each field and calendar for the calendar
        private void ForEachControl(Action<AxSftDate> field, Action<AxSftCalendar> calendar) {
            foreach (AxSftDate f in fields)
                field(f);
            calendar(sftCalendar);
        }

        // calls action for the Calendar object of each control, which holds the calendar settings of the fields' dropdowns too
        private void ForEachCalendar(Action<SftDateCalendar> action) {
            ForEachControl(f => action(f.Calendar), c => action(c.Calendar));
        }

        // ActiveX controls (AxHost) do not take part in the form's automatic scaling, so the form scales their positions and widths
        protected override void ScaleControl(SizeF factor, BoundsSpecified specified) {
            base.ScaleControl(factor, specified);
            foreach (Control c in Controls)
                if (c is AxHost)
                    c.SetBounds((int)Math.Round(c.Left * factor.Width), (int)Math.Round(c.Top * factor.Height), (int)Math.Round(c.Width * factor.Width), c.Height);
        }

        private void Form1_Load(object sender, EventArgs e) {
            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
            DateTime now = DateTime.Now;
            ForEachControl(f => {
                f.DarkMode = SftDateDarkModeConstants.darkSftDateAuto;
                f.HighContrastMode = SftDateHighContrastConstants.highContrastSftDateAuto;
                f.ImageScaling = SftDateScalingConstants.scalingSftDateStretch;
                if (f != fieldEmpty && f != fieldRange)
                    f.Value = now;
            }, c => {
                c.DarkMode = SftDateDarkModeConstants.darkSftDateAuto;
                c.HighContrastMode = SftDateHighContrastConstants.highContrastSftDateAuto;
                c.ImageScaling = SftDateScalingConstants.scalingSftDateStretch;
                c.Value = now;
            });
            fieldRange.SetRange(now, now.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.\r\n\r\n" +
                "The options apply to all fields and calendars; the month counts apply to the SftCalendar control.";

            // the options start out with the controls' settings
            SftDateCalendar cal = sftCalendar.Calendar;
            comboCulture.Items.AddRange(cultures);
            comboCulture.SelectedIndex = 0;
            comboDarkMode.Items.AddRange(new object[] { "Off", "On", "Auto" });
            comboDarkMode.SelectedIndex = (int)sftCalendar.DarkMode;
            comboHighContrast.Items.AddRange(new object[] { "Off", "On", "Auto" });
            comboHighContrast.SelectedIndex = (int)sftCalendar.HighContrastMode;
            comboButtons.Items.AddRange(new object[] { "Dropdown", "Up/down", "Dropdown, up/down", "None" });
            comboButtons.SelectedIndex = (int)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 = (int)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 = !cal.ShowWeekNumbers ? 0 : cal.WeekNumberRule == SftDateWeekRuleConstants.weekRuleSftDateISO8601 ? 2 : 1;
            comboFirstDay.Items.AddRange(new object[] { "(Culture)", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" });
            comboFirstDay.SelectedIndex = (int)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();
        }

        // The calendar sizes itself to its months, so its group box, the selection text, the Close button and the form follow it.
        private void DoLayout() {
            int margin = groupFields.Left;
            sftCalendar.Location = new Point(groupCalendar.Left + margin, groupCalendar.Top + 2 * margin);
            int width = 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);
            int right = groupCalendar.Right + margin;
            int bottom = Math.Max(groupCalendar.Bottom, labelHelp.Bottom) + margin;
            buttonClose.Location = new Point(right - margin - buttonClose.Width, bottom);
            ClientSize = new Size(right, buttonClose.Bottom + margin);
        }

        private string FormatLongDate(DateTime date) {
            Culture culture = cultures[comboCulture.SelectedIndex];
            CultureInfo info = (CultureInfo)(culture.Locale.Length == 0 ? CultureInfo.CurrentCulture : new CultureInfo(culture.Locale)).Clone();
            if (culture.Calendar == SftDateCalendarTypeConstants.calendarSftDateJapanese)
                info.DateTimeFormat.Calendar = new JapaneseCalendar();
            else if (culture.Calendar == SftDateCalendarTypeConstants.calendarSftDateHebrew)
                info.DateTimeFormat.Calendar = new HebrewCalendar();
            return date.ToString("D", info);
        }

        private void ShowSelection() {
            string text = "Selected: (none)";
            if (sftCalendar.SelectionMode == SftDateSelectionModeConstants.selectionSftDateRange) {
                if (sftCalendar.RangeStart is DateTime) {
                    DateTime start = (DateTime)sftCalendar.RangeStart;
                    DateTime end = sftCalendar.RangeEnd is DateTime ? (DateTime)sftCalendar.RangeEnd : start;
                    text = "Selected: " + FormatLongDate(start) + " - " + FormatLongDate(end);
                }
            } else if (sftCalendar.Value is DateTime)
                text = "Selected: " + FormatLongDate((DateTime)sftCalendar.Value);
            labelSelection.Text = text;
        }

        private void sftCalendar_ValueChanged(object sender, EventArgs e) {
            ShowSelection();
        }

        // Colors set by the application are used as given in dark mode too, so the scheme has a variant for each mode.
        private void SetColorScheme(SftDateCalendar cal, bool dark, bool on) {
            cal.TitleBackColor = on ? Ole(Color.FromArgb(0, 95, 115)) : DefaultColor;
            cal.TitleForeColor = on ? Ole(Color.White) : DefaultColor;
            cal.DayOfWeekForeColor = !on ? DefaultColor : dark ? Ole(Color.FromArgb(110, 190, 205)) : Ole(Color.FromArgb(0, 95, 115));
            cal.WeekNumberForeColor = !on ? DefaultColor : dark ? Ole(Color.FromArgb(110, 190, 205)) : Ole(Color.FromArgb(0, 95, 115));
            cal.TodayColor = on ? Ole(Color.FromArgb(225, 140, 30)) : DefaultColor;
            cal.SelectedBackColor = on ? Ole(Color.FromArgb(225, 140, 30)) : DefaultColor;
            cal.SelectedForeColor = on ? Ole(Color.White) : DefaultColor;
            cal.RangeBackColor = !on ? DefaultColor : dark ? Ole(Color.FromArgb(95, 60, 20)) : Ole(Color.FromArgb(250, 225, 190));
        }

        private void SetColorScheme(AxSftDate field, bool on) {
            SetColorScheme(field.Calendar, field.DarkModeActive, on);
            ISftDate ocx = (ISftDate)field.GetOcx();
            ocx.SegmentHighlightBackColor = on ? Ole(Color.FromArgb(225, 140, 30)) : DefaultColor;
            ocx.SegmentHighlightForeColor = on ? Ole(Color.White) : DefaultColor;
        }

        private void SetColorSchemes() {
            ForEachControl(f => SetColorScheme(f, checkColors.Checked), c => SetColorScheme(c.Calendar, c.DarkModeActive, checkColors.Checked));
        }

        private void field_DarkModeChange(object sender, _ISftDateEvents_DarkModeChangeEvent e) {
            SetColorScheme((AxSftDate)sender, checkColors.Checked);
        }

        private void sftCalendar_DarkModeChange(object sender, _ISftCalendarEvents_DarkModeChangeEvent e) {
            SetColorScheme(sftCalendar.Calendar, e.active, checkColors.Checked);
        }

        // Special dates repeat every year, month or week, and can be bold, colored, disabled, show a picture and a tooltip.
        private void SetHolidays(bool show) {
            DateTime today = DateTime.Today;
            DateTime monday = today.AddDays(-(((int)today.DayOfWeek + 6) % 7));
            ForEachCalendar(cal => {
                SftDateSpecialDates dates = cal.SpecialDates;
                dates.Clear();
                if (!show)
                    return;
                SftDateSpecialDate day = dates.Add(new DateTime(today.Year, 1, 1), SftDateRecurrenceConstants.recurrenceSftDateAnnually);
                day.Bold = true;
                day.ForeColor = Ole(Color.Firebrick);
                day.ToolTip = "New Year's Day";
                day = dates.Add(new DateTime(today.Year, 7, 4), SftDateRecurrenceConstants.recurrenceSftDateAnnually);
                day.Bold = true;
                day.ForeColor = Ole(Color.Firebrick);
                day.ToolTip = "Independence Day";
                day = dates.Add(new DateTime(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 DateTime(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";
            });
        }

        // any day of any calendar can be changed here; this colors weekends that aren't special dates
        private void DayFormatting(SftDateDayFormatting day) {
            if (checkWeekends.Checked && day.Weekend && day.SpecialDate < 0)
                day.ForeColor = Ole(Color.IndianRed);
        }

        private void field_DayFormatting(object sender, _ISftDateEvents_DayFormattingEvent e) {
            DayFormatting(e.day);
        }

        private void sftCalendar_DayFormatting(object sender, _ISftCalendarEvents_DayFormattingEvent e) {
            DayFormatting(e.day);
        }

        private void option_Changed(object sender, EventArgs e) {
            if (!initialized)
                return;
            if (sender == comboCulture) {
                Culture culture = cultures[comboCulture.SelectedIndex];
                bool rtl = (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(f => {
                    f.CalendarType = SftDateCalendarTypeConstants.calendarSftDateDefault;
                    f.Culture = culture.Locale;
                    f.CalendarType = culture.Calendar;
                    f.CtlRightToLeft = rtl;
                }, c => {
                    c.CalendarType = SftDateCalendarTypeConstants.calendarSftDateDefault;
                    c.Culture = culture.Locale;
                    c.CalendarType = culture.Calendar;
                    c.CtlRightToLeft = rtl;
                });
                ShowSelection();
            } else if (sender == comboDarkMode) {
                SftDateDarkModeConstants mode = (SftDateDarkModeConstants)comboDarkMode.SelectedIndex;
                ForEachControl(f => f.DarkMode = mode, c => c.DarkMode = mode);
                SetColorSchemes();
            } else if (sender == comboHighContrast) {
                SftDateHighContrastConstants mode = (SftDateHighContrastConstants)comboHighContrast.SelectedIndex;
                ForEachControl(f => f.HighContrastMode = mode, c => c.HighContrastMode = mode);
            } else if (sender == comboButtons) {
                foreach (AxSftDate f in fields)
                    f.Buttons = (SftDateButtonsConstants)comboButtons.SelectedIndex;
            } else if (sender == comboBorder) {
                foreach (AxSftDate f in fields)
                    f.BorderStyle = borderStyles[comboBorder.SelectedIndex];
            } else if (sender == comboAlignment) {
                foreach (AxSftDate f in fields)
                    f.DropDownAlignment = (SftDateAlignConstants)comboAlignment.SelectedIndex;
            } else if (sender == comboAcross) {
                sftCalendar.Calendar.MonthsAcross = comboAcross.SelectedIndex + 1;
            } else if (sender == comboDown) {
                sftCalendar.Calendar.MonthsDown = comboDown.SelectedIndex + 1;
            } else if (sender == comboWeekNumbers) {
                ForEachCalendar(cal => {
                    cal.ShowWeekNumbers = comboWeekNumbers.SelectedIndex > 0;
                    cal.WeekNumberRule = comboWeekNumbers.SelectedIndex == 2 ? SftDateWeekRuleConstants.weekRuleSftDateISO8601 : SftDateWeekRuleConstants.weekRuleSftDateCulture;
                });
            } else if (sender == comboFirstDay) {
                ForEachCalendar(cal => cal.FirstDayOfWeek = (SftDateFirstDayConstants)comboFirstDay.SelectedIndex);
            } else if (sender == comboViews) {
                ForEachCalendar(cal => cal.AllowedViews = viewSets[comboViews.SelectedIndex]);
            } else if (sender == checkTrailing) {
                ForEachCalendar(cal => cal.ShowTrailingDates = checkTrailing.Checked);
            } else if (sender == checkTodayButton) {
                ForEachCalendar(cal => cal.ShowTodayButton = checkTodayButton.Checked);
            } else if (sender == checkHighlightToday) {
                ForEachCalendar(cal => cal.HighlightToday = checkHighlightToday.Checked);
            } else if (sender == checkClearButton) {
                ForEachCalendar(cal => cal.ShowClearButton = checkClearButton.Checked);
            } else if (sender == checkHolidays) {
                SetHolidays(checkHolidays.Checked);
            } else if (sender == checkWeekends) {
                // the DayFormatting event colors the days; RefreshDays asks for them again
                ForEachCalendar(cal => cal.RefreshDays());
            } else if (sender == checkDisableWeekends) {
                ForEachCalendar(cal => cal.DisabledDaysOfWeek = checkDisableWeekends.Checked ? SftDateDaysConstants.daysSftDateWeekend : SftDateDaysConstants.daysSftDateNone);
            } else if (sender == checkRangeSelect) {
                sftCalendar.SelectionMode = checkRangeSelect.Checked ? SftDateSelectionModeConstants.selectionSftDateRange : SftDateSelectionModeConstants.selectionSftDateSingle;
                ShowSelection();
            } else if (sender == checkThisYear) {
                DateTime first = new DateTime(DateTime.Today.Year, 1, 1), last = new DateTime(DateTime.Today.Year, 12, 31);
                ForEachControl(f => {
                    f.MinDate = checkThisYear.Checked ? first : minDate;
                    f.MaxDate = checkThisYear.Checked ? last : maxDate;
                }, c => {
                    c.MinDate = checkThisYear.Checked ? first : minDate;
                    c.MaxDate = checkThisYear.Checked ? last : maxDate;
                });
                ShowSelection();
            } else if (sender == checkDropOnFocus) {
                foreach (AxSftDate f in fields)
                    f.DropOnFocus = checkDropOnFocus.Checked;
            } else if (sender == checkAutoAdvance) {
                foreach (AxSftDate f in fields)
                    f.AutoAdvance = checkAutoAdvance.Checked;
            } else if (sender == checkColors) {
                SetColorSchemes();
            } else if (sender == checkThemes) {
                ForEachControl(f => f.UseThemes = checkThemes.Checked, c => c.UseThemes = checkThemes.Checked);
            }
            DoLayout();
        }

        private void buttonClose_Click(object sender, EventArgs e) {
            Close();
        }
    }
}

Last Updated 11/29/2026 - (email)
© 2026 Softel vdm, Inc.