Пример #1
0
        /// <summary>
        /// Converts the specified two-digit year to a four-digit year by using the Globalization.PersianCalendar.TwoDigitYearMax property to determine the appropriate century.
        /// </summary>
        /// <param name="year">A two-digit integer that represents the year to convert.</param>
        /// <returns>An integer that contains the four-digit representation of year.</returns>
        /// <remarks>TwoDigitYearMax is the last year in the 100-year range that can be represented by a two-digit year. The century is determined by finding the sole occurrence of the two-digit year within that 100-year range. For example, if TwoDigitYearMax is set to 1429, the 100-year range is from 1330 to 1429; therefore, a 2-digit value of 30 is interpreted as 1330, while a 2-digit value of 29 is interpreted as 1429.</remarks>
        public override int ToFourDigitYear(int year)
        {
            if (year != 0)
            {
                try
                {
                    CheckYearRange(true, year);
                }
                catch (ArgumentOutOfRangeException)
                {
                    //throw new System.ArgumentOutOfRangeException("Year", year, ResourceLibrary.GetString(CalendarKeys.InvalidYear, CalendarKeys.Root));
                    throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidFourDigitYear));
                }
            }

            if (year > 99)
            {
                return(year);
            }
            int a = TwoDigitYearMax / 100;

            if (year > TwoDigitYearMax - a * 100)
            {
                a--;
            }
            return(a * 100 + year);
        }
 protected override void OnValueValidating(ValueValidatingEventArgs e)
 {
     try
     {
         string txt = e.Value.ToString();
         if (string.IsNullOrEmpty(txt) || txt == FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.Validation_NullText))
         {
             e.HasError = false;
         }
         else if (mv.MonthViewControl.DefaultCulture.Equals(mv.MonthViewControl.PersianCulture))
         {
             PersianDate pd = PersianDate.Parse(txt, GetFormatByFormatInfo(FormatInfo));
             e.HasError = false;
             mv.MonthViewControl.IsNull = false;
             mv.MonthViewControl.SelectedDateTime = pd;
         }
         else if (mv.MonthViewControl.DefaultCulture.Equals(mv.MonthViewControl.InvariantCulture))
         {
             DateTime dt = DateTime.Parse(txt);
             e.HasError = false;
             mv.MonthViewControl.IsNull = false;
             mv.MonthViewControl.SelectedDateTime = dt;
         }
     }
     catch (Exception)
     {
         e.HasError = true;
         mv.MonthViewControl.IsNull = true;
     }
 }
Пример #3
0
        /// <summary>
        /// Returns a DateTime that is the specified number of years away from the specified DateTime.
        /// </summary>
        /// <param name="time">The DateTime instance to add.</param>
        /// <param name="years">The number of years to add.</param>
        /// <returns>The DateTime that results from adding the specified number of years to the specified DateTime.</returns>
        /// <remarks>
        /// The day part of the resulting DateTime is affected if the resulting day is not a valid day in the resulting month of the resulting year; it is changed to the last valid day in the resulting month of the resulting year. The time-of-day part of the resulting DateTime remains the same as the specified DateTime.
        ///
        /// For example, Esfand has 29 days, except during leap years when it has 30 days. If the specified Date is the 30th day of Esfand in a leap year and the value of years is 1, the resulting Date will be the 29th day of Esfand in the following year.
        ///
        /// If years is negative, the resulting DateTime would be earlier than the specified DateTime.
        /// </remarks>
        public override DateTime AddYears(DateTime time, int years)
        {
            int year  = GetYear(true, time);
            int month = GetMonth(false, time);
            int day   = GetDayOfMonth(false, time);

            year += years;
            if (day == 30 && month == 12)
            {
                if (!IsLeapYear(false, year, 0))
                {
                    day = 29;
                }
            }
            DateTime dateTime;

            try
            {
                dateTime = ToDateTime(year, month, day, 0, 0, 0, 0) + time.TimeOfDay;
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidDateTime));
            }

            return(dateTime);
        }
        /// <summary>
        /// Use this method to get equivalants of each available responses returned by control.
        /// </summary>
        /// <param name="EnglishButtonText"></param>
        /// <returns></returns>
        internal static string GetFAMessageBoxButton(string EnglishButtonText)
        {
            switch (EnglishButtonText.ToUpper())
            {
            case "OK":
                return(FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.MessageBox_Ok));

            case "CANCEL":
                return(FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.MessageBox_Cancel));

            case "YES":
                return(FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.MessageBox_Yes));

            case "NO":
                return(FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.MessageBox_No));

            case "ABORT":
                return(FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.MessageBox_Abort));

            case "RETRY":
                return(FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.MessageBox_Retry));

            case "IGNORE":
                return(FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.MessageBox_Ignore));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #5
0
        /// <summary>
        /// Returns a DateTime that is set to the specified Date and time in the specified era.
        /// </summary>
        /// <param name="year">An integer that represents the year.</param>
        /// <param name="month">An integer that represents the month.</param>
        /// <param name="day">An integer that represents the day.</param>
        /// <param name="hour">An integer that represents the hour.</param>
        /// <param name="minute">An integer that represents the minute.</param>
        /// <param name="second">An integer that represents the second.</param>
        /// <param name="millisecond">An integer that represents the millisecond.</param>
        /// <param name="era">An integer that represents the era.</param>
        /// <returns>The DateTime instance set to the specified Date and time in the current era.</returns>
        public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
        {
            CheckEraRange(true, era);
            CheckDateRange(true, year, month, day);
            int days = day;

            for (int i = 1; i < month; i++)
            {
                if (i < 7)
                {
                    days += 31;
                }
                else if (i < 12)
                {
                    days += 30;
                }
            }
            days += 365 * year + NumberOfLeapYearsUntil(false, year);
            // following line validates the arguments of time
            DateTime timePart = new DateTime(1, 1, 1, hour, minute, second, millisecond);
            long     ticks    = days * 864000000000L + timePart.Ticks + 195721056000000000L;
            DateTime dateTime;

            try
            {
                dateTime = new DateTime(ticks);
            }
            catch (ArgumentOutOfRangeException)
            {
                // If ticks go greater than DateTime.MaxValue.Ticks, this exception will be caught
                throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidMonthDay));
            }

            return(dateTime);
        }
Пример #6
0
        /// <summary>
        /// Returns a DateTime that is the specified number of months away from the specified DateTime.
        /// </summary>
        /// <param name="time">The DateTime instance to add.</param>
        /// <param name="months">The number of months to add.</param>
        /// <returns>The DateTime that results from adding the specified number of months to the specified DateTime.</returns>
        /// <remarks>
        /// The year part of the resulting DateTime is affected if the resulting month is beyond the last month of the current year. The day part of the resulting DateTime is also affected if the resulting day is not a valid day in the resulting month of the resulting year; it is changed to the last valid day in the resulting month of the resulting year. The time-of-day part of the resulting DateTime remains the same as the specified DateTime.
        ///
        /// For example, if the specified month is Ordibehesht, which is the 2nd month and has 31 days, the specified day is the 31th day of that month, and the value of the months parameter is -3, the resulting year is one less than the specified year, the resulting month is Bahman, and the resulting day is the 30th day, which is the last day in Bahman.
        ///
        /// If the value of the months parameter is negative, the resulting DateTime would be earlier than the specified DateTime.
        /// </remarks>
        public override DateTime AddMonths(DateTime time, int months)
        {
            if (Math.Abs(months) > 120000)
            {
                throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidMonth));
            }

            int year  = GetYear(true, time);
            int month = GetMonth(false, time);
            int day   = GetDayOfMonth(false, time);

            month += (year - 1) * 12 + months;
            year   = (month - 1) / 12 + 1;
            month -= (year - 1) * 12;
            if (day > 29)
            {
                int maxday = GetDaysInMonth(false, year, month, 0);
                if (maxday < day)
                {
                    day = maxday;
                }
            }
            DateTime dateTime;

            try
            {
                dateTime = ToDateTime(year, month, day, 0, 0, 0, 0) + time.TimeOfDay;
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidDateTime));
            }

            return(dateTime);
        }
        /// <summary>
        /// Creates a new instance of FADatePickerConverter class.
        /// </summary>
        public FADatePickerConverter()
        {
            RightToLeftChanged += new EventHandler(OnInternalRightToLeftChanged);
            ThemeChanged += new EventHandler(OnInternalThemeChanged);
            PopupShowing += new EventHandler(OnInternalPopupShowing);
            mv = new FAMonthViewContainer(this);
            mv.MonthViewControl.SelectedDateTimeChanged += new EventHandler(OnMVSelectedDateTimeChanged);
            mv.MonthViewControl.ButtonClicked += new CalendarButtonClickedEventHandler(OnMVButtonClicked);
            FALocalizeManager.LocalizerChanged += new EventHandler(OnInternalLocalizerChanged);
            FAThemeManager.ManagerThemeChanged += new EventHandler(OnInternalManagerThemeChanged);
            convert = new Button();
            convert.Size = new Size(DEF_CONVERT_WIDTH, Height - 4);
            convert.Image = ButtonImage;
            convert.TabStop = false;
            convert.Click += new EventHandler(OnConvertButtonClick);
            convert.GotFocus += new EventHandler(OnConvertButtonFocus);
            Controls.Add(convert);

            FormatInfo = FormatInfoTypes.ShortDate;

            if (mv.MonthViewControl.DefaultCulture.Equals(mv.MonthViewControl.PersianCulture))
            {
                calType = CalendarTypes.Persian;
            }
            else
            {
                calType = CalendarTypes.English;
            }

            Text = FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.Validation_NullText);
            UpdateButtons();
        }
        private void SetSelectedDateTime(DateTime dt)
        {
            DateTime oldValue = selectedDateTime;
            DateTime newValue = dt;

            SelectedDateTimeChangingEventArgs changeArgs = new SelectedDateTimeChangingEventArgs(oldValue, newValue);
            OnSelectedDateTimeChanging(changeArgs);

            if (changeArgs.Cancel)
            {
                if (string.IsNullOrEmpty(changeArgs.Message))
                    Error.SetError(this, FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.Validation_Cancel));
                else
                    Error.SetError(this, changeArgs.Message);

                return;
            }

            if (!string.IsNullOrEmpty(changeArgs.Message))
            {
                Error.SetError(this, changeArgs.Message);
            }
            else
            {
                Error.SetError(this, string.Empty);
            }

            selectedDateTime = dt;
            OnSelectedDateTimeChanged(EventArgs.Empty);

            UpdateTextValue();
        }
Пример #9
0
        /// <summary>
        /// Converts a number in string format e.g. 14500 to its localized version, if <c>Localized</c> value is set to <c>true</c>.
        /// </summary>
        /// <param name="EnglishNumber"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public static string Convert(string EnglishNumber, CultureInfo culture)
        {
            string numEnglish = "";
            string numTemp    = "";

            for (int i = 0; i < EnglishNumber.Length; i++)
            {
                numTemp = EnglishNumber.Substring(i, 1);
                switch (numTemp)
                {
                case "0":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_0);
                    break;

                case "1":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_1);
                    break;

                case "2":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_2);
                    break;

                case "3":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_3);
                    break;

                case "4":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_4);
                    break;

                case "5":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_5);
                    break;

                case "6":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_6);
                    break;

                case "7":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_7);
                    break;

                case "8":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_8);
                    break;

                case "9":
                    numEnglish = numEnglish + FALocalizeManager.GetLocalizerByCulture(culture).GetLocalizedString(StringID.Numbers_9);
                    break;

                default:
                    numEnglish = numEnglish + numTemp;
                    break;
                }
            }

            return(numEnglish);
        }
Пример #10
0
 private void CheckMonthRange(bool validate, int month)
 {
     if (validate)
     {
         if (month < 1 || 12 < month)
         {
             throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidMonth));
         }
     }
     return;
 }
Пример #11
0
 private void CheckYearRange(bool validate, int year)
 {
     if (validate)
     {
         if (year < 1 || 9378 < year)
         {
             throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidYear));
         }
     }
     return;
 }
Пример #12
0
 private void CheckEraRange(bool validate, int era)
 {
     if (validate)
     {
         if (era < 0 || 1 < era)
         {
             throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidEra));
         }
     }
     return;
 }
Пример #13
0
 private void CheckTicksRange(bool validate, DateTime time)
 {
     // Valid ticks represent times between 12:00:00.000 AM, 22/03/0622 CE and 11:59:59.999 PM, 31/12/9999 CE.
     if (validate)
     {
         long ticks = time.Ticks;
         if (ticks < 196037280000000000L)
         {
             throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidDateTime));
         }
     }
     return;
 }
Пример #14
0
        /// <summary>
        /// Creates a new instance of FADatePicker class.
        /// </summary>
        public FADatePicker()
        {
            mv = new FAMonthViewContainer(this);
            RightToLeftChanged += new EventHandler(OnInternalRightToLeftChanged);
            mv.MonthViewControl.SelectedDateTimeChanged += new EventHandler(OnMVSelectedDateTimeChanged);
            mv.MonthViewControl.ButtonClicked           += new CalendarButtonClickedEventHandler(OnMVButtonClicked);
            FALocalizeManager.LocalizerChanged          += new EventHandler(OnInternalLocalizerChanged);

            PopupShowing += new EventHandler(OnInternalPopupShowing);
            Text          = FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.Validation_NullText);

            FormatInfo = FormatInfoTypes.ShortDate;
        }
Пример #15
0
 private void CheckDateRange(bool validate, int year, int month, int day)
 {
     if (validate)
     {
         int maxday = GetDaysInMonth(true, year, month, 0);
         if (day < 1 || maxday < day)
         {
             if (day == 30 && month == 12)
             {
                 throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidLeapYear));
             }
             else
             {
                 throw new InvalidPersianDateException(FALocalizeManager.GetLocalizerByCulture(System.Threading.Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.PersianDate_InvalidDay));
             }
         }
     }
 }
 /// <summary>
 /// Updates text representation of the selected value
 /// </summary>
 public override void UpdateTextValue()
 {
     if (mv.MonthViewControl.IsNull)
     {
         Text = FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.Validation_NullText);
     }
     else
     {
         if (mv.MonthViewControl.DefaultCulture.Equals(mv.MonthViewControl.PersianCulture))
         {
             Text = ((PersianDate)SelectedDateTime).ToString(GetFormatByFormatInfo(FormatInfo));
         }
         else
         {
             Text = SelectedDateTime.ToString(GetFormatByFormatInfo(FormatInfo), mv.MonthViewControl.DefaultCulture);
         }
     }
 }
        private void OnConvertButtonClick(object sender, EventArgs e)
        {
            OnConvertButtonClicked(EventArgs.Empty);

            if (Text == null || Text == FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.Validation_NullText) || Text.Length == 0)
                return;

            if (CalendarType == CalendarTypes.Persian)
            {
                CalendarType = CalendarTypes.English;
            }
            else
            {
                CalendarType = CalendarTypes.Persian;
            }

            try
            {
                if (CalendarType == CalendarTypes.Persian)
                {
                    mv.MonthViewControl.DefaultCalendar = mv.MonthViewControl.PersianCalendar;
                    mv.MonthViewControl.DefaultCulture = mv.MonthViewControl.PersianCulture;
                    DateTime dt = SelectedDateTime;

                    Text = ((PersianDate)dt).ToString(GetFormatByFormatInfo(FormatInfo));
                }
                else
                {
                    mv.MonthViewControl.DefaultCalendar = mv.MonthViewControl.InvariantCalendar;
                    mv.MonthViewControl.DefaultCulture = mv.MonthViewControl.InvariantCulture;
                    DateTime dt = SelectedDateTime;

                    Text = dt.ToString(GetFormatByFormatInfo(FormatInfo), mv.MonthViewControl.DefaultCulture);
                }
            }
            catch (FormatException)
            {
                Text = FALocalizeManager.GetLocalizerByCulture(Thread.CurrentThread.CurrentUICulture).GetLocalizedString(StringID.Validation_NullText);
                mv.MonthViewControl.SetNoneDay();
            }
        }