This sample shows labeled fields of every kind and a calendar, with options that change their culture and calendar system, format, buttons, months, week numbers, special dates, dark mode, high contrast and other properties.
The source code is located at C:\Program Files (x86)\Softelvdm\SftDate NET 3.0\Samples\CSharp\DemoSample\Form1.cs.
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Softelvdm.SftDateNET;
using Softelvdm.SftDateNET.Controls;
namespace DemoSample {
public partial class Form1 : Form {
private SftDate[] fields;
public Form1() {
InitializeComponent();
fields = new SftDate[] { sftDate1, sftDate2, sftDate3, sftDate4, sftDate5, sftDate6, sftDate7, sftDate8, sftDate9 };
comboCulture.Items.AddRange(new object[] {
"(Windows setting)",
"English (United States)",
"German (Germany)",
"French (France)",
"Japanese (Japan), Japanese calendar",
"Arabic (Saudi Arabia), Hijri calendar",
"Hebrew (Israel), Hebrew calendar",
"Thai (Thailand), Buddhist calendar",
"Persian (Iran), Persian calendar",
});
comboCulture.SelectedIndex = 0;
FillCombo(comboDarkMode, typeof(DarkModeEnum), sftCalendar1.DarkMode);
FillCombo(comboHighContrast, typeof(HighContrastModeEnum), sftCalendar1.HighContrastMode);
FillCombo(comboButtons, typeof(FieldButtonsEnum), sftDate1.Buttons);
comboWeekNumbers.Items.AddRange(new object[] { "None", "Culture rule", "ISO 8601" });
comboWeekNumbers.SelectedIndex = 0;
FillCombo(comboFirstDay, typeof(FirstDayOfWeekEnum), sftCalendar1.Calendar.FirstDayOfWeek);
ShowSelection();
}
private void FillCombo(ComboBox combo, Type enumType, object current) {
foreach (object value in Enum.GetValues(enumType))
combo.Items.Add(value);
combo.SelectedItem = current;
}
// The options apply to all fields and the calendar; a field's Calendar property defines its dropdown calendar.
private void ForEachCalendar(Action<CalendarClass> action) {
foreach (SftDate field in fields)
action(field.Calendar);
action(sftCalendar1.Calendar);
}
private CultureInfo SelectedCulture() {
CultureInfo culture;
switch (comboCulture.SelectedIndex) {
case 1: return new CultureInfo("en-US");
case 2: return new CultureInfo("de-DE");
case 3: return new CultureInfo("fr-FR");
case 4:
culture = new CultureInfo("ja-JP");
culture.DateTimeFormat.Calendar = new JapaneseCalendar();
return culture;
case 5: return new CultureInfo("ar-SA");
case 6:
culture = new CultureInfo("he-IL");
culture.DateTimeFormat.Calendar = new HebrewCalendar();
return culture;
case 7: return new CultureInfo("th-TH");
case 8:
culture = new CultureInfo("fa-IR");
culture.DateTimeFormat.Calendar = new PersianCalendar();
return culture;
}
return null;
}
private void comboCulture_SelectedIndexChanged(object sender, EventArgs e) {
CultureInfo culture = SelectedCulture();
RightToLeft rtl = culture != null && culture.TextInfo.IsRightToLeft ? RightToLeft.Yes : RightToLeft.No;
foreach (SftDate field in fields) {
field.Culture = culture;
field.RightToLeft = rtl;
}
sftCalendar1.Culture = culture;
sftCalendar1.RightToLeft = rtl;
ShowSelection();
}
private void comboDarkMode_SelectedIndexChanged(object sender, EventArgs e) {
DarkModeEnum mode = (DarkModeEnum)comboDarkMode.SelectedItem;
foreach (SftDate field in fields)
field.DarkMode = mode;
sftCalendar1.DarkMode = mode;
}
private void comboHighContrast_SelectedIndexChanged(object sender, EventArgs e) {
HighContrastModeEnum mode = (HighContrastModeEnum)comboHighContrast.SelectedItem;
foreach (SftDate field in fields)
field.HighContrastMode = mode;
sftCalendar1.HighContrastMode = mode;
}
private void comboButtons_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftDate field in fields)
field.Buttons = (FieldButtonsEnum)comboButtons.SelectedItem;
}
private void numericMonths_ValueChanged(object sender, EventArgs e) {
sftCalendar1.Calendar.MonthsAcross = (int)numericMonths.Value;
}
private void comboWeekNumbers_SelectedIndexChanged(object sender, EventArgs e) {
int index = comboWeekNumbers.SelectedIndex;
ForEachCalendar(delegate (CalendarClass calendar) {
calendar.ShowWeekNumbers = index > 0;
calendar.WeekNumberRule = index == 2 ? WeekNumberRuleEnum.Iso8601 : WeekNumberRuleEnum.Culture;
});
}
private void comboFirstDay_SelectedIndexChanged(object sender, EventArgs e) {
FirstDayOfWeekEnum day = (FirstDayOfWeekEnum)comboFirstDay.SelectedItem;
ForEachCalendar(delegate (CalendarClass calendar) { calendar.FirstDayOfWeek = day; });
}
private void checkTrailing_CheckedChanged(object sender, EventArgs e) {
ForEachCalendar(delegate (CalendarClass calendar) { calendar.ShowTrailingDates = checkTrailing.Checked; });
}
private void checkToday_CheckedChanged(object sender, EventArgs e) {
ForEachCalendar(delegate (CalendarClass calendar) { calendar.ShowTodayButton = checkToday.Checked; });
}
private void checkClear_CheckedChanged(object sender, EventArgs e) {
ForEachCalendar(delegate (CalendarClass calendar) { calendar.ShowClearButton = checkClear.Checked; });
}
// Special dates repeat every year, month or week, and can be bold, colored, disabled or show a tooltip.
private void checkHolidays_CheckedChanged(object sender, EventArgs e) {
int year = DateTime.Today.Year;
DateTime monday = DateTime.Today.AddDays(-(((int)DateTime.Today.DayOfWeek + 6) % 7));
ForEachCalendar(delegate (CalendarClass calendar) {
calendar.SpecialDates.Clear();
if (!checkHolidays.Checked)
return;
SpecialDateClass day = calendar.SpecialDates.Add(new DateTime(year, 1, 1), RecurrenceEnum.Annually);
day.Bold = true;
day.ForeColor = Color.Firebrick;
day.ToolTip = "New Year's Day";
day = calendar.SpecialDates.Add(new DateTime(year, 7, 4), RecurrenceEnum.Annually);
day.Bold = true;
day.ForeColor = Color.Firebrick;
day.ToolTip = "Independence Day";
day = calendar.SpecialDates.Add(new DateTime(year, 12, 25), RecurrenceEnum.Annually);
day.Bold = true;
day.ForeColor = Color.Firebrick;
day.BackColor = Color.MistyRose;
day.ToolTip = "Christmas Day";
day = calendar.SpecialDates.Add(new DateTime(year, 1, 15), RecurrenceEnum.Monthly);
day.Bold = false;
day.Enabled = false;
day.ToolTip = "Closed for inventory";
day = calendar.SpecialDates.Add(monday, RecurrenceEnum.Weekly);
day.Bold = false;
day.ForeColor = Color.RoyalBlue;
day.ToolTip = "Team meeting";
});
}
// The DayFormatting event can change any day; here weekends without a special date are colored.
private void AnyControl_DayFormatting(object sender, DayFormattingEventArgs e) {
if (checkWeekends.Checked && e.IsWeekend && e.SpecialDate == null)
e.ForeColor = Color.IndianRed;
}
private void checkWeekends_CheckedChanged(object sender, EventArgs e) {
sftCalendar1.RefreshDays();
}
private void checkRange_CheckedChanged(object sender, EventArgs e) {
sftCalendar1.SelectionMode = checkRange.Checked ? SelectionModeEnum.Range : SelectionModeEnum.Single;
ShowSelection();
}
private void checkDropOnFocus_CheckedChanged(object sender, EventArgs e) {
foreach (SftDate field in fields)
field.DropOnFocus = checkDropOnFocus.Checked;
}
private void sftCalendar1_SelectionChanged(object sender, EventArgs e) {
ShowSelection();
}
private void ShowSelection() {
CultureInfo culture = sftCalendar1.Culture ?? CultureInfo.CurrentCulture;
string text;
if (sftCalendar1.SelectionMode == SelectionModeEnum.Range)
text = sftCalendar1.RangeStart.HasValue
? sftCalendar1.RangeStart.Value.ToString("D", culture) + " - " + (sftCalendar1.RangeEnd ?? sftCalendar1.RangeStart).Value.ToString("D", culture)
: "(none)";
else
text = sftCalendar1.Value.HasValue ? sftCalendar1.Value.Value.ToString("D", culture) : "(none)";
labelSelection.Text = "Selected: " + text;
}
}
}