/// <summary> /// Gets the last date of the week specified by this instance. /// </summary> /// <param name="provider">The format provider.</param> /// <returns>The last date in the week.</returns> public MonthCalendarDate GetEndDateOfWeek(ICustomFormatProvider provider) { DayOfWeek currentDayOfWeek = this.DayOfWeek; DayOfWeek firstDayOfWeek = provider.FirstDayOfWeek; if (currentDayOfWeek == firstDayOfWeek) { return(this.AddDays(6)); } int d = (int)currentDayOfWeek; int daysToAdd = -1; while (currentDayOfWeek != firstDayOfWeek) { daysToAdd++; if (++d > 6) { d = 0; } currentDayOfWeek = (DayOfWeek)d; } return(this.AddDays(daysToAdd)); }
/// <summary> /// Gets the abbreviated and day names, beginning with the first day provided by the <paramref name="provider"/>. /// </summary> /// <param name="provider">The <see cref="ICustomFormatProvider"/> to use.</param> /// <returns>The abbreviated and day names beginning with the first day.</returns> public static string[,] GetDayNames(ICustomFormatProvider provider) { List <string> abbDayNames = new List <string>(provider.AbbreviatedDayNames); List <string> shortestDayNames = new List <string>(provider.ShortestDayNames); List <string> dayNames = new List <string>(provider.DayNames); string firstDayName = provider.GetAbbreviatedDayName(provider.FirstDayOfWeek); int firstNameIndex = abbDayNames.IndexOf(firstDayName); string[,] names = new string[3, dayNames.Count]; int j = 0; for (int i = firstNameIndex; i < abbDayNames.Count; i++, j++) { names[0, j] = dayNames[i]; names[1, j] = abbDayNames[i]; names[2, j] = shortestDayNames[i]; } for (int i = 0; i < firstNameIndex; i++, j++) { names[0, j] = dayNames[i]; names[1, j] = abbDayNames[i]; names[2, j] = shortestDayNames[i]; } return(names); }
/// <summary> /// Gets the abbreviated and day names, beginning with the first day provided by the <paramref name="provider"/>. /// </summary> /// <param name="provider">The <see cref="ICustomFormatProvider"/> to use.</param> /// <returns>The abbreviated and day names beginning with the first day.</returns> public static string[,] GetDayNames(ICustomFormatProvider provider) { List<string> abbDayNames = new List<string>(provider.AbbreviatedDayNames); List<string> shortestDayNames = new List<string>(provider.ShortestDayNames); List<string> dayNames = new List<string>(provider.DayNames); string firstDayName = provider.GetAbbreviatedDayName(provider.FirstDayOfWeek); int firstNameIndex = abbDayNames.IndexOf(firstDayName); string[,] names = new string[3, dayNames.Count]; int j = 0; for (int i = firstNameIndex; i < abbDayNames.Count; i++, j++) { names[0, j] = dayNames[i]; names[1, j] = abbDayNames[i]; names[2, j] = shortestDayNames[i]; } for (int i = 0; i < firstNameIndex; i++, j++) { names[0, j] = dayNames[i]; names[1, j] = abbDayNames[i]; names[2, j] = shortestDayNames[i]; } return names; }
/// <summary> /// Gets the first day in the week. /// </summary> /// <param name="provider">The format provider.</param> /// <returns>The first day in the week specified by this instance.</returns> public MonthCalendarDate GetFirstDayInWeek(ICustomFormatProvider provider) { DayOfWeek firstDayOfWeek = provider.FirstDayOfWeek; MonthCalendarDate dt = (MonthCalendarDate)this.Clone(); while (dt.DayOfWeek != firstDayOfWeek && dt.Date > this.calendar.MinSupportedDateTime) { dt = dt.AddDays(-1); } return(dt); }
/// <summary> /// Returns a string representation of the native <see cref="Calendar"/> dependent date using the <see cref="IFormatProvider"/> and <paramref name="format"/> /// for formatting. /// </summary> /// <param name="format">A string which holds the format pattern.</param> /// <param name="fp">The <see cref="IFormatProvider"/> to use for formatting.</param> /// <param name="nameProvider">The day and month name provider.</param> /// <param name="nativeDigits">If not <c>null</c> and valid, uses for number representation the specified native digits.</param> /// <returns> /// A string representation of this object. /// </returns> public string ToString(string format, IFormatProvider fp, ICustomFormatProvider nameProvider = null, string[] nativeDigits = null) { if (fp != null) { DateTimeFormatInfo dtfi = DateTimeFormatInfo.GetInstance(fp); if (dtfi != null) { return(this.ToString(format, dtfi, nameProvider, nativeDigits)); } } return(this.ToString(format, null, nameProvider, nativeDigits)); }
/// <summary> /// Initializes a new instance of the <see cref="DatePatternParser"/> class. /// </summary> /// <param name="pattern">The date pattern.</param> /// <param name="provider">The format provider.</param> /// <exception cref="ArgumentNullException">If <paramref name="provider"/> is <c>null</c>.</exception> /// <exception cref="InvalidOperationException">If <paramref name="pattern"/> is <c>null</c> or empty.</exception> public DatePatternParser(string pattern, ICustomFormatProvider provider) { if (string.IsNullOrEmpty(pattern)) { throw new InvalidOperationException("parameter 'pattern' cannot be null or empty."); } if (provider == null) { throw new ArgumentNullException("provider", "parameter 'provider' cannot be null."); } this.provider = provider; this.pattern = pattern; }
/// <summary> /// Gets the abbreviated or day names, beginning with the first day of the week specified by the <paramref name="provider"/>. /// </summary> /// <param name="provider">The provider to use.</param> /// <param name="index">The index for the kind of name : 0 means full, 1 means abbreviated and 2 means shortest day names.</param> /// <returns>The (abbreviated) day names.</returns> public static string[] GetDayNames(ICustomFormatProvider provider, int index) { if (index < 0 || index > 2) { index = 0; } string[,] dayNames = GetDayNames(provider); string[] names = new string[dayNames.GetLength(1)]; for (int i = 0; i < names.Length; i++) { names[i] = dayNames[index, i]; } return(names); }
/// <summary> /// Gets the abbreviated or day names, beginning with the first day of the week specified by the <paramref name="provider"/>. /// </summary> /// <param name="provider"> /// The provider to use. /// </param> /// <param name="index"> /// The index for the kind of name : 0 means full, 1 means abbreviated and 2 means shortest day names. /// </param> /// <returns> /// The (abbreviated) day names. /// </returns> public static string[] GetDayNames(ICustomFormatProvider provider, int index) { if (index < 0 || index > 2) { index = 0; } string[,] dayNames = GetDayNames(provider); string[] names = new string[dayNames.GetLength(1)]; for (int i = 0; i < names.Length; i++) { names[i] = dayNames[index, i]; } return names; }
/// <summary> /// Returns a string representation of this object. /// </summary> /// <param name="format"> /// The format pattern. /// </param> /// <param name="dtfi"> /// The <see cref="DateTimeFormatInfo"/> to use for formatting. /// </param> /// <param name="nameProvider"> /// The day and month name provider. /// </param> /// <param name="nativeDigits"> /// If not <c>null</c> and valid, uses for number representation the specified native digits. /// </param> /// <returns> /// A <see cref="System.String"/> that represents this instance. /// </returns> private string ToString(string format, DateTimeFormatInfo dtfi, ICustomFormatProvider nameProvider, string[] nativeDigits = null) { if (dtfi == null) { dtfi = CultureInfo.CurrentUICulture.DateTimeFormat; } if (string.IsNullOrEmpty(format)) { format = nameProvider != null ? nameProvider.ShortDatePattern : dtfi.ShortDatePattern; } else if (format.Length == 1) { switch (format[0]) { case 'D': { format = nameProvider != null ? nameProvider.LongDatePattern : dtfi.LongDatePattern; break; } case 'm': case 'M': { format = nameProvider != null ? nameProvider.MonthDayPattern : dtfi.MonthDayPattern; break; } case 'y': case 'Y': { format = nameProvider != null ? nameProvider.YearMonthPattern : dtfi.YearMonthPattern; break; } default: { format = nameProvider != null ? nameProvider.ShortDatePattern : dtfi.ShortDatePattern; break; } } } format = format.Replace(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator, "/"); StringBuilder sb = new StringBuilder(); Calendar c = nameProvider != null ? nameProvider.Calendar : dtfi.Calendar; int i = 0; while (i < format.Length) { int tokLen; char ch = format[i]; switch (ch) { case 'd': { tokLen = CountChar(format, i, ch); sb.Append( tokLen <= 2 ? DateMethods.GetNumberString(this.Day, nativeDigits, tokLen == 2) : GetDayName(c.GetDayOfWeek(this.Date), dtfi, nameProvider, tokLen == 3)); break; } case 'M': { tokLen = CountChar(format, i, ch); sb.Append( tokLen <= 2 ? DateMethods.GetNumberString(this.Month, nativeDigits, tokLen == 2) : GetMonthName(this.Month, this.Year, dtfi, nameProvider, tokLen == 3)); break; } case 'y': { tokLen = CountChar(format, i, ch); sb.Append( tokLen <= 2 ? DateMethods.GetNumberString(this.Year % 100, nativeDigits, true) : DateMethods.GetNumberString(this.Year, nativeDigits, false)); break; } case 'g': { tokLen = CountChar(format, i, ch); sb.Append(nameProvider != null ? nameProvider.GetEraName(c.GetEra(this.Date)) : dtfi.GetEraName(c.GetEra(this.Date))); break; } case '/': { tokLen = CountChar(format, i, ch); sb.Append(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator); break; } case '\'': { tokLen = 1; break; } default: { tokLen = 1; sb.Append(ch.ToString(CultureInfo.CurrentUICulture)); break; } } i += tokLen; } return sb.ToString(); }
/// <summary> /// Initializes a new instance of the <see cref="EnhancedMonthCalendar"/> class. /// </summary> public EnhancedMonthCalendar() { this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true); this.InitializeComponent(); this._eraRanges = GetEraRanges(this.cultureCalendar); this._formatProvider = new MonthCalendarFormatProvider(this.culture, null, this.culture.TextInfo.IsRightToLeft) { EnhancedMonthCalendar = this }; this._minDate = this.cultureCalendar.MinSupportedDateTime.Date < new DateTime(1900, 1, 1) ? new DateTime(1900, 1, 1) : this.cultureCalendar.MinSupportedDateTime.Date; this._maxDate = this.cultureCalendar.MaxSupportedDateTime.Date > new DateTime(9998, 12, 31) ? new DateTime(9998, 12, 31) : this.cultureCalendar.MaxSupportedDateTime.Date; this._renderer = new MonthCalendarRenderer(this); this.UpdateYearMenu(DateTime.Today.Year); // update year menu this.SetStartDate(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)); // set start date this.CalculateSize(this.SizeMode == CalendarSizeMode.CalendarSize); }
/// <summary> /// Gets the abbreviated or full day name for the specified <see cref="DayOfWeek"/> value using, if not null, the <paramref name="nameProvider"/> or /// the <see cref="DateTimeFormatInfo"/> specified by <paramref name="info"/>. /// </summary> /// <param name="dayofweek"> /// The <see cref="DayOfWeek"/> value to get the day name for. /// </param> /// <param name="info"> /// The <see cref="DateTimeFormatInfo"/> to get the name. /// </param> /// <param name="nameProvider"> /// The <see cref="ICustomFormatProvider"/> to get the name. This parameter has precedence before the /// <paramref name="info"/>. Can be <c>null</c>. /// </param> /// <param name="abbreviated"> /// true to get the abbreviated day name; false otherwise. /// </param> /// <returns> /// The full or abbreviated day name specified by <paramref name="dayofweek"/> value. /// </returns> private static string GetDayName(DayOfWeek dayofweek, DateTimeFormatInfo info, ICustomFormatProvider nameProvider, bool abbreviated) { if (nameProvider != null) { return abbreviated ? nameProvider.GetAbbreviatedDayName(dayofweek) : nameProvider.GetDayName(dayofweek); } return abbreviated ? info.GetAbbreviatedDayName(dayofweek) : info.GetDayName(dayofweek); }
/// <summary> /// Gets the abbreviated or full month name for the specified <paramref name="month"/> and <paramref name="year"/> value, using the /// <paramref name="nameProvider"/> if not null or the <see cref="DateTimeFormatInfo"/> specified by <paramref name="info"/>. /// </summary> /// <param name="month"> /// The month value to get the month name for. /// </param> /// <param name="year"> /// The year value to get the month name for. /// </param> /// <param name="info"> /// The <see cref="DateTimeFormatInfo"/> value to use. /// </param> /// <param name="nameProvider"> /// The <see cref="ICustomFormatProvider"/> to get the name. This parameter has precedence before the /// <paramref name="info"/>. Can be <c>null</c>. /// </param> /// <param name="abbreviated"> /// true to get the abbreviated month name; false otherwise. /// </param> /// <returns> /// The full or abbreviated month name specified by <paramref name="month"/> and <paramref name="year"/>. /// </returns> private static string GetMonthName(int month, int year, DateTimeFormatInfo info, ICustomFormatProvider nameProvider, bool abbreviated) { if (nameProvider != null) { return abbreviated ? nameProvider.GetAbbreviatedMonthName(year, month) : nameProvider.GetMonthName(year, month); } return abbreviated ? info.GetAbbreviatedMonthName(month) : info.GetMonthName(month); }
/// <summary> /// Gets the first day in the week. /// </summary> /// <param name="provider"> /// The format provider. /// </param> /// <returns> /// The first day in the week specified by this instance. /// </returns> public MonthCalendarDate GetFirstDayInWeek(ICustomFormatProvider provider) { DayOfWeek firstDayOfWeek = provider.FirstDayOfWeek; MonthCalendarDate dt = (MonthCalendarDate)this.Clone(); while (dt.DayOfWeek != firstDayOfWeek && dt.Date > this._calendar.MinSupportedDateTime) { dt = dt.AddDays(-1); } return dt; }
/// <summary> /// Returns a string representation of the native <see cref="Calendar"/> dependent date using the <see cref="IFormatProvider"/> and /// <paramref name="format"/> /// for formatting. /// </summary> /// <param name="format"> /// A string which holds the format pattern. /// </param> /// <param name="fp"> /// The <see cref="IFormatProvider"/> to use for formatting. /// </param> /// <param name="nameProvider"> /// The day and month name provider. /// </param> /// <param name="nativeDigits"> /// If not <c>null</c> and valid, uses for number representation the specified native digits. /// </param> /// <returns> /// A string representation of this object. /// </returns> public string ToString(string format, IFormatProvider fp, ICustomFormatProvider nameProvider = null, string[] nativeDigits = null) { if (fp != null) { DateTimeFormatInfo dtfi = DateTimeFormatInfo.GetInstance(fp); if (dtfi != null) { return this.ToString(format, dtfi, nameProvider, nativeDigits); } } return this.ToString(format, null, nameProvider, nativeDigits); }
/// <summary> /// Returns a string representation of this object. /// </summary> /// <param name="format">The format pattern.</param> /// <param name="dtfi">The <see cref="DateTimeFormatInfo"/> to use for formatting.</param> /// <param name="nameProvider">The day and month name provider.</param> /// <param name="nativeDigits">If not <c>null</c> and valid, uses for number representation the specified native digits.</param> /// <returns> /// A <see cref="System.String"/> that represents this instance. /// </returns> private string ToString(string format, DateTimeFormatInfo dtfi, ICustomFormatProvider nameProvider, string[] nativeDigits = null) { if (dtfi == null) { dtfi = CultureInfo.CurrentUICulture.DateTimeFormat; } if (string.IsNullOrEmpty(format)) { format = nameProvider != null ? nameProvider.ShortDatePattern : dtfi.ShortDatePattern; } else if (format.Length == 1) { switch (format[0]) { case 'D': { format = nameProvider != null ? nameProvider.LongDatePattern : dtfi.LongDatePattern; break; } case 'm': case 'M': { format = nameProvider != null ? nameProvider.MonthDayPattern : dtfi.MonthDayPattern; break; } case 'y': case 'Y': { format = nameProvider != null ? nameProvider.YearMonthPattern : dtfi.YearMonthPattern; break; } default: { format = nameProvider != null ? nameProvider.ShortDatePattern : dtfi.ShortDatePattern; break; } } } format = format.Replace(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator, "/"); StringBuilder sb = new StringBuilder(); Calendar c = nameProvider != null ? nameProvider.Calendar : dtfi.Calendar; int i = 0; while (i < format.Length) { int tokLen; char ch = format[i]; switch (ch) { case 'd': { tokLen = CountChar(format, i, ch); sb.Append(tokLen <= 2 ? DateMethods.GetNumberString(this.Day, nativeDigits, tokLen == 2) : GetDayName(c.GetDayOfWeek(this.Date), dtfi, nameProvider, tokLen == 3)); break; } case 'M': { tokLen = CountChar(format, i, ch); sb.Append(tokLen <= 2 ? DateMethods.GetNumberString(this.Month, nativeDigits, tokLen == 2) : GetMonthName(this.Month, this.Year, dtfi, nameProvider, tokLen == 3)); break; } case 'y': { tokLen = CountChar(format, i, ch); sb.Append(tokLen <= 2 ? DateMethods.GetNumberString(this.Year % 100, nativeDigits, true) : DateMethods.GetNumberString(this.Year, nativeDigits, false)); break; } case 'g': { tokLen = CountChar(format, i, ch); sb.Append(nameProvider != null ? nameProvider.GetEraName(c.GetEra(this.Date)) : dtfi.GetEraName(c.GetEra(this.Date))); break; } case '/': { tokLen = CountChar(format, i, ch); sb.Append(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator); break; } case '\'': { tokLen = 1; break; } default: { tokLen = 1; sb.Append(ch.ToString(CultureInfo.CurrentUICulture)); break; } } i += tokLen; } return(sb.ToString()); }
/// <summary> /// Gets the last date of the week specified by this instance. /// </summary> /// <param name="provider"> /// The format provider. /// </param> /// <returns> /// The last date in the week. /// </returns> public MonthCalendarDate GetEndDateOfWeek(ICustomFormatProvider provider) { DayOfWeek currentDayOfWeek = this.DayOfWeek; DayOfWeek firstDayOfWeek = provider.FirstDayOfWeek; if (currentDayOfWeek == firstDayOfWeek) { return this.AddDays(6); } int d = (int)currentDayOfWeek; int daysToAdd = -1; while (currentDayOfWeek != firstDayOfWeek) { daysToAdd++; if (++d > 6) { d = 0; } currentDayOfWeek = (DayOfWeek)d; } return this.AddDays(daysToAdd); }
/// <summary> /// Raises the <see cref="System.Windows.Forms.Control.Paint"/> event. /// </summary> /// <param name="e">A <see cref="PaintEventArgs"/> that contains the event data.</param> protected override void OnPaint(PaintEventArgs e) { if (this.inEditMode) { e.Graphics.Clear(this.BackColor); base.OnPaint(e); return; } e.Graphics.Clear(this.Enabled ? (this.isValidDate ? this.BackColor : this.invalidDateBackColor) : SystemColors.Window); using (StringFormat format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.NoClip | StringFormatFlags.NoWrap)) { format.LineAlignment = StringAlignment.Center; if (this.RightToLeft == RightToLeft.Yes) { format.Alignment = StringAlignment.Far; } using (SolidBrush foreBrush = new SolidBrush(this.Enabled ? (this.isValidDate ? this.ForeColor : this.invalidDateForeColor) : SystemColors.GrayText), selectedBrush = new SolidBrush(SystemColors.HighlightText), selectedBack = new SolidBrush(SystemColors.Highlight)) { MonthCalendar cal = this.datePicker.Picker; ICustomFormatProvider provider = cal.FormatProvider; MonthCalendarDate date = new MonthCalendarDate(cal.CultureCalendar, this.currentDate); DatePatternParser parser = new DatePatternParser(provider.ShortDatePattern, provider); string dateString = parser.ParsePattern(date, this.datePicker.UseNativeDigits ? this.datePicker.Culture.NumberFormat.NativeDigits : null); this.dayPartIndex = parser.DayPartIndex; this.monthPartIndex = parser.MonthPartIndex; this.yearPartIndex = parser.YearPartIndex; List <CharacterRange> rangeList = new List <CharacterRange>(); int dayIndex = parser.DayIndex; int monthIndex = parser.MonthIndex; int yearIndex = parser.YearIndex; if (!string.IsNullOrEmpty(parser.DayString)) { rangeList.Add(new CharacterRange(dayIndex, parser.DayString.Length)); } if (!string.IsNullOrEmpty(parser.MonthString)) { rangeList.Add(new CharacterRange(monthIndex, parser.MonthString.Length)); } if (!string.IsNullOrEmpty(parser.YearString)) { rangeList.Add(new CharacterRange(yearIndex, parser.YearString.Length)); } format.SetMeasurableCharacterRanges(rangeList.ToArray()); Rectangle layoutRect = this.ClientRectangle; e.Graphics.DrawString(dateString, this.Font, foreBrush, layoutRect, format); Region[] dateRegions = e.Graphics.MeasureCharacterRanges(dateString, this.Font, layoutRect, format); this.dayBounds = dateRegions[0].GetBounds(e.Graphics); this.monthBounds = dateRegions[1].GetBounds(e.Graphics); this.yearBounds = dateRegions[2].GetBounds(e.Graphics); if (this.selectedPart == SelectedDatePart.Day) { e.Graphics.FillRectangle(selectedBack, this.dayBounds.X, this.dayBounds.Y - 2, this.dayBounds.Width + 1, this.dayBounds.Height + 1); e.Graphics.DrawString(parser.DayString, this.Font, selectedBrush, this.dayBounds.X - 2, this.dayBounds.Y - 2); } if (this.selectedPart == SelectedDatePart.Month) { e.Graphics.FillRectangle(selectedBack, this.monthBounds.X, this.monthBounds.Y - 2, this.monthBounds.Width + 1, this.monthBounds.Height + 1); e.Graphics.DrawString(parser.MonthString, this.Font, selectedBrush, this.monthBounds.X - 2, this.monthBounds.Y - 2); } if (this.selectedPart == SelectedDatePart.Year) { e.Graphics.FillRectangle(selectedBack, this.yearBounds.X, this.yearBounds.Y - 2, this.yearBounds.Width + 1, this.yearBounds.Height + 1); e.Graphics.DrawString(parser.YearString, this.Font, selectedBrush, this.yearBounds.X - 2, this.yearBounds.Y - 2); } } } base.OnPaint(e); }
/// <summary> /// Returns a string representation of this object. /// </summary> /// <param name="format">The format pattern.</param> /// <param name="dtfi">The <see cref="DateTimeFormatInfo"/> to use for formatting.</param> /// <param name="nameProvider">The day and month name provider.</param> /// <returns></returns> private string ToString(string format, DateTimeFormatInfo dtfi, ICustomFormatProvider nameProvider) { if (dtfi == null) { dtfi = CultureInfo.CurrentUICulture.DateTimeFormat; } bool rtlCulture = nameProvider != null ? nameProvider.IsRTLLanguage : false; if (string.IsNullOrEmpty(format)) { format = nameProvider != null ? nameProvider.ShortDatePattern : dtfi.ShortDatePattern; } else if (format.Length == 1) { switch (format[0]) { case 'D': { format = nameProvider != null ? nameProvider.LongDatePattern : dtfi.LongDatePattern; break; } case 'm': case 'M': { format = nameProvider != null ? nameProvider.MonthDayPattern : dtfi.MonthDayPattern; break; } case 'y': case 'Y': { format = nameProvider != null ? nameProvider.YearMonthPattern : dtfi.YearMonthPattern; break; } default: { format = nameProvider != null ? nameProvider.YearMonthPattern : dtfi.ShortDatePattern; break; } } } format = format.Replace(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator, "/"); StringBuilder sb = new StringBuilder(); const string rtlCode = "\u200F"; const string ltrCode = "\u200E"; Calendar c = nameProvider != null ? nameProvider.Calendar : dtfi.Calendar; int i = 0; while (i < format.Length) { int tokLen; char ch = format[i]; switch (ch) { case 'd': { tokLen = CountChar(format, i, ch); if (tokLen <= 2) { sb.Append(rtlCulture ? rtlCode : ltrCode).Append(tokLen == 2 ? this.Day.ToString("##00") : this.Day.ToString()); } else { sb.Append(rtlCulture ? rtlCode : ltrCode).Append(GetDayName(c.GetDayOfWeek(this.Date), dtfi, nameProvider, tokLen == 3)); } break; } case 'M': { tokLen = CountChar(format, i, ch); if (tokLen <= 2) { sb.Append(rtlCulture ? rtlCode : ltrCode).Append(tokLen == 2 ? this.Month.ToString("##00") : this.Month.ToString()); } else { sb.Append(rtlCulture ? rtlCode : ltrCode).Append(GetMonthName(this.Month, this.Year, dtfi, nameProvider, tokLen == 3)); } break; } case 'y': { tokLen = CountChar(format, i, ch); if (tokLen <= 2) { sb.Append(rtlCulture ? rtlCode : ltrCode).Append((this.Year % 100).ToString("##00")); } else { sb.Append(rtlCulture ? rtlCode : ltrCode).Append(this.Year.ToString()); } break; } case 'g': { tokLen = CountChar(format, i, ch); sb.Append(rtlCulture ? rtlCode : ltrCode).Append(nameProvider != null ? nameProvider.GetEraName(c.GetEra(this.Date)) : dtfi.GetEraName(c.GetEra(this.Date))); break; } case '/': { tokLen = CountChar(format, i, ch); sb.Append(rtlCulture ? rtlCode : ltrCode).Append(nameProvider != null ? nameProvider.DateSeparator : dtfi.DateSeparator); break; } case '\'': { tokLen = 1; break; } default: { tokLen = 1; sb.Append(rtlCulture ? rtlCode : ltrCode).Append(ch.ToString()); break; } } i += tokLen; } return(sb.ToString()); }
/// <summary> /// Initializes a new instance of the <see cref="MonthCalendar"/> class. /// </summary> public MonthCalendar() { SetStyle( ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true); // initialize menus this.InitializeComponent(); this.extendSelection = false; this.showFooter = true; this.showWeekHeader = true; this.mouseLocation = Point.Empty; this.yearSelected = DateTime.MinValue; this.monthSelected = DateTime.MinValue; this.selectionStart = DateTime.Today; this.selectionEnd = DateTime.Today; this.currentHitType = MonthCalendarHitType.None; this.boldedDates = new List<DateTime>(); this.boldDatesCollection = new BoldedDatesCollection(); this.boldDateCategoryCollection = new BoldedDateCategoryCollection(this); this.currentMoveBounds = Rectangle.Empty; this.mouseMoveFlags = new MonthCalendarMouseMoveFlags(); this.selectionRanges = new List<SelectionRange>(); this.daySelectionMode = MonthCalendarSelectionMode.Manual; this.nonWorkDays = CalendarDayOfWeek.Saturday | CalendarDayOfWeek.Sunday; this.culture = CultureInfo.CurrentUICulture; this.cultureCalendar = CultureInfo.CurrentUICulture.DateTimeFormat.Calendar; this.eraRanges = GetEraRanges(this.cultureCalendar); this.minDate = this.cultureCalendar.MinSupportedDateTime.Date < new DateTime(1900, 1, 1) ? new DateTime(1900, 1, 1) : this.cultureCalendar.MinSupportedDateTime.Date; this.maxDate = this.cultureCalendar.MaxSupportedDateTime.Date > new DateTime(9998, 12, 31) ? new DateTime(9998, 12, 31) : this.cultureCalendar.MaxSupportedDateTime.Date; this.formatProvider = new MonthCalendarFormatProvider(this.culture, null, this.culture.TextInfo.IsRightToLeft) { MonthCalendar = this }; this.renderer = new MonthCalendarRenderer(this); this.calendarDimensions = new Size(1, 1); this.headerFont = new Font("Segoe UI", 9f, FontStyle.Regular); this.footerFont = new Font("Arial", 9f, FontStyle.Bold); this.dayHeaderFont = new Font("Segoe UI", 8f, FontStyle.Regular); this.dayTextAlign = ContentAlignment.MiddleCenter; // update year menu this.UpdateYearMenu(DateTime.Today.Year); // set start date this.SetStartDate(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)); this.CalculateSize(true); }
/// <summary> /// Gets the abbreviated or full month name for the specified <paramref name="month"/> and <paramref name="year"/> value, /// using the <paramref name="nameProvider"/> if not null or the <see cref="DateTimeFormatInfo"/> specified by <paramref name="info"/>. /// </summary> /// <param name="month">The month value to get the month name for.</param> /// <param name="year">The year value to get the month name for.</param> /// <param name="info">The <see cref="DateTimeFormatInfo"/> value to use.</param> /// <param name="nameProvider">The <see cref="ICustomFormatProvider"/> to get the name. /// This parameter has precedence before the <paramref name="info"/>. Can be <c>null</c>.</param> /// <param name="abbreviated">true to get the abbreviated month name; false otherwise.</param> /// <returns>The full or abbreviated month name specified by <paramref name="month"/> and <paramref name="year"/>.</returns> private static string GetMonthName(int month, int year, DateTimeFormatInfo info, ICustomFormatProvider nameProvider, bool abbreviated) { if (nameProvider != null) { return(abbreviated ? nameProvider.GetAbbreviatedMonthName(year, month) : nameProvider.GetMonthName(year, month)); } return(abbreviated ? info.GetAbbreviatedMonthName(month) : info.GetMonthName(month)); }
/// <summary> /// Gets the abbreviated or full day name for the specified <see cref="DayOfWeek"/> value using, /// if not null, the <paramref name="nameProvider"/> or the <see cref="DateTimeFormatInfo"/> specified by <paramref name="info"/>. /// </summary> /// <param name="dayofweek">The <see cref="DayOfWeek"/> value to get the day name for.</param> /// <param name="info">The <see cref="DateTimeFormatInfo"/> to get the name.</param> /// <param name="nameProvider">The <see cref="ICustomFormatProvider"/> to get the name. /// This parameter has precedence before the <paramref name="info"/>. Can be <c>null</c>.</param> /// <param name="abbreviated">true to get the abbreviated day name; false otherwise.</param> /// <returns>The full or abbreviated day name specified by <paramref name="dayofweek"/> value.</returns> private static string GetDayName(DayOfWeek dayofweek, DateTimeFormatInfo info, ICustomFormatProvider nameProvider, bool abbreviated) { if (nameProvider != null) { return(abbreviated ? nameProvider.GetAbbreviatedDayName(dayofweek) : nameProvider.GetDayName(dayofweek)); } return(abbreviated ? info.GetAbbreviatedDayName(dayofweek) : info.GetDayName(dayofweek)); }