Ejemplo n.º 1
0
 private void Select(DateTimeInfo info)
 {
     _fireSelectionChangedEvent = false;
     TextBox.Select(info.StartPosition, info.Length);
     _fireSelectionChangedEvent = true;
     _selectedDateTimeInfo      = info;
 }
Ejemplo n.º 2
0
        protected override void OnValueChanged(DateTime?oldValue, DateTime?newValue)
        {
            DateTimeInfo info = _selectedDateTimeInfo;

            //this only occurs when the user manually type in a value for the Value Property
            if (info == null)
            {
                info = (this.CurrentDateTimePart != DateTimePart.Other) ? this.GetDateTimeInfo(this.CurrentDateTimePart) : _dateTimeInfoList[0];
            }

            //whenever the value changes we need to parse out the value into out DateTimeInfo segments so we can keep track of the individual pieces
            //but only if it is not null
            if (newValue != null)
            {
                ParseValueIntoDateTimeInfo(this.Value);
            }

            base.OnValueChanged(oldValue, newValue);

            if (!_isTextChangedFromUI)
            {
                _lastValidDate = newValue;
            }

            if (TextBox != null)
            {
                //we loose our selection when the Value is set so we need to reselect it without firing the selection changed event
                _fireSelectionChangedEvent = false;
                TextBox.Select(info.StartPosition, info.Length);
                _fireSelectionChangedEvent = true;
            }
        }
Ejemplo n.º 3
0
        private DateTimeInfo GetPreviousDateTimeInfo(int previousSelectionStart)
        {
            DateTimeInfo previousDateTimeInfo = this.GetDateTimeInfo(previousSelectionStart);

            if (previousDateTimeInfo == null)
            {
                previousDateTimeInfo = _dateTimeInfoList.Last();
            }

            DateTimeInfo initialDateTimeInfo = previousDateTimeInfo;

            while (previousDateTimeInfo.Type == DateTimePart.Other)
            {
                previousDateTimeInfo = this.GetDateTimeInfo(previousDateTimeInfo.StartPosition - 1);
                if (previousDateTimeInfo == null)
                {
                    previousDateTimeInfo = _dateTimeInfoList.Last();
                }
                if (object.Equals(previousDateTimeInfo, initialDateTimeInfo))
                {
                    throw new InvalidOperationException("Couldn't find a valid DateTimeInfo.");
                }
            }
            return(previousDateTimeInfo);
        }
Ejemplo n.º 4
0
        private DateTimeInfo GetNextDateTimeInfo(int nextSelectionStart)
        {
            DateTimeInfo nextDateTimeInfo = this.GetDateTimeInfo(nextSelectionStart);

            if (nextDateTimeInfo == null)
            {
                nextDateTimeInfo = _dateTimeInfoList.First();
            }

            DateTimeInfo initialDateTimeInfo = nextDateTimeInfo;

            while (nextDateTimeInfo.Type == DateTimePart.Other)
            {
                nextDateTimeInfo = this.GetDateTimeInfo(nextDateTimeInfo.StartPosition + nextDateTimeInfo.Length);
                if (nextDateTimeInfo == null)
                {
                    nextDateTimeInfo = _dateTimeInfoList.First();
                }
                if (object.Equals(nextDateTimeInfo, initialDateTimeInfo))
                {
                    throw new InvalidOperationException("Couldn't find a valid DateTimeInfo.");
                }
            }
            return(nextDateTimeInfo);
        }
Ejemplo n.º 5
0
        private void UpdateTimeSpan(int value)
        {
            _fireSelectionChangedEvent = false;
            DateTimeInfo info = _selectedDateTimeInfo;

            //this only occurs when the user manually type in a value for the Value Property
            if (info == null)
            {
                info = _dateTimeInfoList[0];
            }

            TimeSpan?result = null;

            try
            {
                switch (info.Type)
                {
                case DateTimePart.Day:
                    result = (( TimeSpan )Value).Add(new TimeSpan(value, 0, 0, 0, 0));
                    break;

                case DateTimePart.Hour24:
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, value, 0, 0, 0));
                    break;

                case DateTimePart.Minute:
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, 0, value, 0, 0));
                    break;

                case DateTimePart.Second:
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, 0, 0, value, 0));
                    break;

                case DateTimePart.Millisecond:
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, 0, 0, 0, value));
                    break;

                default:
                    break;
                }
            }
            catch
            {
                //this can occur if the date/time = 1/1/0001 12:00:00 AM which is the smallest date allowed.
                //I could write code that would validate the date each and everytime but I think that it would be more
                //efficient if I just handle the edge case and allow an exeption to occur and swallow it instead.
            }

            result = ((result != null) && result.HasValue)
                ? result.Value.TotalMilliseconds > 0 ? result.Value : this.DefaultValue
                : result;

            this.Value = this.CoerceValueMinMax(result);

            //we loose our selection when the Value is set so we need to reselect it without firing the selection changed event
            this.TextBox.Select(info.StartPosition, info.Length);
            _fireSelectionChangedEvent = true;
        }
Ejemplo n.º 6
0
        private void Increment(int step)
        {
            // if UpdateValueOnEnterKey is true,
            // Sync Value on Text only when Enter Key is pressed.
            if (this.UpdateValueOnEnterKey)
            {
                _fireSelectionChangedEvent = false;

                var currentValue = this.ConvertTextToValue(this.TextBox.Text);
                if (currentValue.HasValue)
                {
                    var newValue = this.UpdateDateTime(currentValue, step);
                    this.TextBox.Text = newValue.Value.ToString(this.GetFormatString(this.Format), this.CultureInfo);
                }
                else
                {
                    this.TextBox.Text = (this.DefaultValue != null)
                              ? this.DefaultValue.Value.ToString(this.GetFormatString(this.Format), this.CultureInfo)
                              : this.ContextNow.ToString(this.GetFormatString(this.Format), this.CultureInfo);
                }

                if (this.TextBox != null)
                {
                    DateTimeInfo info = _selectedDateTimeInfo;
                    //this only occurs when the user manually type in a value for the Value Property
                    if (info == null)
                    {
                        info = (this.CurrentDateTimePart != DateTimePart.Other) ? this.GetDateTimeInfo(this.CurrentDateTimePart) : _dateTimeInfoList[0];
                    }

                    //whenever the value changes we need to parse out the value into out DateTimeInfo segments so we can keep track of the individual pieces
                    this.ParseValueIntoDateTimeInfo(this.ConvertTextToValue(this.TextBox.Text));

                    //we loose our selection when the Value is set so we need to reselect it without firing the selection changed event
                    this.TextBox.Select(info.StartPosition, info.Length);
                }
                _fireSelectionChangedEvent = true;
            }
            else
            {
                if (this.Value.HasValue)
                {
                    this.Value = this.UpdateDateTime(this.Value, step);
                }
                else
                {
                    this.Value = this.DefaultValue ?? this.ContextNow;
                }
            }
        }
Ejemplo n.º 7
0
        internal override void Select(DateTimeInfo info)
        {
            if (this.UpdateValueOnEnterKey)
            {
                if ((info != null) && !info.Equals(_selectedDateTimeInfo) && (this.TextBox != null) && !string.IsNullOrEmpty(this.TextBox.Text))
                {
                    var separatorToSkipCount = _dateTimeInfoList.IndexOf(info) / 2;
                    if (separatorToSkipCount < 0)
                    {
                        base.Select(info);
                    }
                    else
                    {
                        var textValues      = this.Text.Split(new char[] { ':', '.' });
                        var selectionStart  = textValues.Take(separatorToSkipCount).Sum(x => x.Length) + separatorToSkipCount;
                        var selectionLength = textValues[separatorToSkipCount].Length;
                        // Do not select the "-" sign when moving selection with arrows.
                        if ((separatorToSkipCount == 0) && (textValues.First().StartsWith("-")))
                        {
                            selectionStart  += 1;
                            selectionLength -= 1;
                        }

                        _fireSelectionChangedEvent = false;
                        this.TextBox.Select(selectionStart, selectionLength);
                        _fireSelectionChangedEvent = true;
                        _selectedDateTimeInfo      = info;
#if VS2008
                        this.CurrentDateTimePart = info.Type;
#else
                        this.SetCurrentValue(DateTimeUpDownBase <TimeSpan?> .CurrentDateTimePartProperty, info.Type);
#endif
                    }
                }
            }
            else
            {
                base.Select(info);
            }
        }
        private TimeSpan?UpdateTimeSpan(TimeSpan?currentValue, int value)
        {
            DateTimeInfo info = _selectedDateTimeInfo;

            //this only occurs when the user manually type in a value for the Value Property
            if (info == null)
            {
                info = (this.CurrentDateTimePart != DateTimePart.Other)
               ? this.GetDateTimeInfo(this.CurrentDateTimePart)
               : (_dateTimeInfoList[0].Content != "-") ? _dateTimeInfoList[0] : _dateTimeInfoList[1];       //Skip negative sign
            }

            TimeSpan?result = null;

            try
            {
                switch (info.Type)
                {
                case DateTimePart.Day:
                    result = (( TimeSpan )currentValue).Add(new TimeSpan(value, 0, 0, 0, 0));
                    break;

                case DateTimePart.Hour24:
                    result = (( TimeSpan )currentValue).Add(new TimeSpan(0, value, 0, 0, 0));
                    break;

                case DateTimePart.Minute:
                    result = (( TimeSpan )currentValue).Add(new TimeSpan(0, 0, value, 0, 0));
                    break;

                case DateTimePart.Second:
                    result = (( TimeSpan )currentValue).Add(new TimeSpan(0, 0, 0, value, 0));
                    break;

                case DateTimePart.Millisecond:
                    switch (this.FractionalSecondsDigitsCount)
                    {
                    case 1:
                        value = value * 100;
                        break;

                    case 2:
                        value = value * 10;
                        break;

                    default:
                        value = value * 1;
                        break;
                    }
                    result = (( TimeSpan )currentValue).Add(new TimeSpan(0, 0, 0, 0, value));
                    break;

                default:
                    break;
                }
            }
            catch
            {
                //this can occur if the date/time = 1/1/0001 12:00:00 AM which is the smallest date allowed.
                //I could write code that would validate the date each and everytime but I think that it would be more
                //efficient if I just handle the edge case and allow an exeption to occur and swallow it instead.
            }

            result = ((result != null) && result.HasValue)
                ? result.Value
                : result;

            result = this.CoerceValueMinMax(result);

            return(result);
        }
Ejemplo n.º 9
0
        private void UpdateTimeSpan(int value)
        {
            _fireSelectionChangedEvent = false;
            DateTimeInfo info = _selectedDateTimeInfo;

            //this only occurs when the user manually type in a value for the Value Property
            if (info == null)
            {
                info = (this.CurrentDateTimePart != DateTimePart.Other)
               ? this.GetDateTimeInfo(this.CurrentDateTimePart)
               : (_dateTimeInfoList[0].Content != "-") ? _dateTimeInfoList[0] : _dateTimeInfoList[1];       //Skip negative sign
            }

            TimeSpan?result = null;

            try
            {
                switch (info.Type)
                {
                case DateTimePart.Day:
                    result = (( TimeSpan )Value).Add(new TimeSpan(value, 0, 0, 0, 0));
                    break;

                case DateTimePart.Hour24:
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, value, 0, 0, 0));
                    break;

                case DateTimePart.Minute:
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, 0, value, 0, 0));
                    break;

                case DateTimePart.Second:
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, 0, 0, value, 0));
                    break;

                case DateTimePart.Millisecond:
                    switch (this.FractionalSecondsDigitsCount)
                    {
                    case 1:
                        value = value * 100;
                        break;

                    case 2:
                        value = value * 10;
                        break;

                    default:
                        value = value * 1;
                        break;
                    }
                    result = (( TimeSpan )Value).Add(new TimeSpan(0, 0, 0, 0, value));
                    break;

                default:
                    break;
                }
            }
            catch
            {
                //this can occur if the date/time = 1/1/0001 12:00:00 AM which is the smallest date allowed.
                //I could write code that would validate the date each and everytime but I think that it would be more
                //efficient if I just handle the edge case and allow an exeption to occur and swallow it instead.
            }

            result = ((result != null) && result.HasValue)
                ? result.Value
                : result;

            var oldValue = this.Value;

            this.Value = this.CoerceValueMinMax(result);

            //we loose our selection when the Value is set so we need to reselect it without firing the selection changed event.
            //For a negative value, add 1 for the minus sign.
            int startPos = (this.GetDateTimeInfo(info.Type) != null) ? this.GetDateTimeInfo(info.Type).StartPosition : info.StartPosition;

            if (result.HasValue && (result.Value.TotalMilliseconds < 0) && oldValue.HasValue && (oldValue.Value.TotalMilliseconds >= 0))
            {
                startPos += 1;
            }
            this.TextBox.Select(startPos, info.Length);
            _fireSelectionChangedEvent = true;
        }
Ejemplo n.º 10
0
        protected override void InitializeDateTimeInfoList()
        {
            DateTimeInfo dayInfo     = _dateTimeInfoList.FirstOrDefault(x => x.Type == DateTimePart.Day);
            bool         hasDay      = dayInfo != null;
            DateTimeInfo negInfo     = _dateTimeInfoList.FirstOrDefault(x => x.Type == DateTimePart.Other);
            bool         hasNegative = (negInfo != null) && (negInfo.Content == "-");

            _dateTimeInfoList.Clear();

            if (this.Value.HasValue && this.Value.Value.TotalMilliseconds < 0)
            {
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Other, Length = 1, Content = "-", IsReadOnly = true
                });
                // Negative has been added, move TextBox.Selection to keep it on current DateTimeInfo
                if (!hasNegative && (this.TextBox != null))
                {
                    this.TextBox.SelectionStart++;
                }
            }

            if (this.Value.HasValue && this.Value.Value.Days != 0)
            {
                int dayLength = this.Value.Value.Days.ToString().Length;
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Day, Length = dayLength, Format = "dd"
                });
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Other, Length = 1, Content = ".", IsReadOnly = true
                });

                // Day has been added, move TextBox.Selection to keep it on current DateTimeInfo
                if (!hasDay && (this.TextBox != null))
                {
                    this.TextBox.SelectionStart += (dayLength + 1);
                }
            }
            // Day has been removed, move TextBox.Selection to keep it on current DateTimeInfo
            else if (hasDay)
            {
                this.TextBox.SelectionStart = Math.Max(0, this.TextBox.SelectionStart - (dayInfo.Length + 1));
            }

            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Hour24, Length = 2, Format = "hh"
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Other, Length = 1, Content = ":", IsReadOnly = true
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Minute, Length = 2, Format = "mm"
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Other, Length = 1, Content = ":", IsReadOnly = true
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Second, Length = 2, Format = "ss"
            });

            if (this.FractionalSecondsDigitsCount > 0)
            {
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Other, Length = 1, Content = ".", IsReadOnly = true
                });
                string fraction = new string( 'f', this.FractionalSecondsDigitsCount );
                //If the "f" custom format specifier is used alone, specify "%f" so that it is not misinterpreted as a standard format string.
                if (fraction.Length == 1)
                {
                    fraction = "%" + fraction;
                }
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Millisecond, Length = this.FractionalSecondsDigitsCount, Format = fraction
                });
            }

            if (this.Value.HasValue)
            {
                this.ParseValueIntoTimeSpanInfo();
            }
        }
Ejemplo n.º 11
0
        private void UpdateDateTime(int value)
        {
            _fireSelectionChangedEvent = false;
            DateTimeInfo info = _selectedDateTimeInfo;

            //this only occurs when the user manually type in a value for the Value Property
            if (info == null)
            {
                info = _dateTimeInfoList[0];
            }

            try
            {
                switch (info.Type)
                {
                case DateTimePart.Year:
                {
                    Value = (( DateTime )Value).AddYears(value);
                    break;
                }

                case DateTimePart.Month:
                case DateTimePart.MonthName:
                {
                    Value = (( DateTime )Value).AddMonths(value);
                    break;
                }

                case DateTimePart.Day:
                case DateTimePart.DayName:
                {
                    Value = (( DateTime )Value).AddDays(value);
                    break;
                }

                case DateTimePart.Hour12:
                case DateTimePart.Hour24:
                {
                    Value = (( DateTime )Value).AddHours(value);
                    break;
                }

                case DateTimePart.Minute:
                {
                    Value = (( DateTime )Value).AddMinutes(value);
                    break;
                }

                case DateTimePart.Second:
                {
                    Value = (( DateTime )Value).AddSeconds(value);
                    break;
                }

                case DateTimePart.Millisecond:
                {
                    Value = (( DateTime )Value).AddMilliseconds(value);
                    break;
                }

                case DateTimePart.AmPmDesignator:
                {
                    Value = (( DateTime )Value).AddHours(value * 12);
                    break;
                }

                default:
                {
                    break;
                }
                }
            }
            catch
            {
                //this can occur if the date/time = 1/1/0001 12:00:00 AM which is the smallest date allowed.
                //I could write code that would validate the date each and everytime but I think that it would be more
                //efficient if I just handle the edge case and allow an exeption to occur and swallow it instead.
            }

            //we loose our selection when the Value is set so we need to reselect it without firing the selection changed event
            TextBox.Select(info.StartPosition, info.Length);
            _fireSelectionChangedEvent = true;
        }
Ejemplo n.º 12
0
        private void InitializeDateTimeInfoList()
        {
            _dateTimeInfoList.Clear();
            _selectedDateTimeInfo = null;

            string format = GetFormatString(Format);

            if (string.IsNullOrEmpty(format))
            {
                return;
            }

            while (format.Length > 0)
            {
                int          elementLength = GetElementLengthByFormat(format);
                DateTimeInfo info          = null;

                switch (format[0])
                {
                case '"':
                case '\'':
                {
                    int closingQuotePosition = format.IndexOf(format[0], 1);
                    info = new DateTimeInfo
                    {
                        IsReadOnly = true,
                        Type       = DateTimePart.Other,
                        Length     = 1,
                        Content    = format.Substring(1, Math.Max(1, closingQuotePosition - 1))
                    };
                    elementLength = Math.Max(1, closingQuotePosition + 1);
                    break;
                }

                case 'D':
                case 'd':
                {
                    string d = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        d = "%" + d;
                    }

                    if (elementLength > 2)
                    {
                        info = new DateTimeInfo
                        {
                            IsReadOnly = true,
                            Type       = DateTimePart.DayName,
                            Format     = d
                        }
                    }
                    ;
                    else
                    {
                        info = new DateTimeInfo
                        {
                            IsReadOnly = false,
                            Type       = DateTimePart.Day,
                            Format     = d
                        }
                    };
                    break;
                }

                case 'F':
                case 'f':
                {
                    string f = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        f = "%" + f;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = false,
                        Type       = DateTimePart.Millisecond,
                        Format     = f
                    };
                    break;
                }

                case 'h':
                {
                    string h = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        h = "%" + h;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = false,
                        Type       = DateTimePart.Hour12,
                        Format     = h
                    };
                    break;
                }

                case 'H':
                {
                    string H = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        H = "%" + H;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = false,
                        Type       = DateTimePart.Hour24,
                        Format     = H
                    };
                    break;
                }

                case 'M':
                {
                    string M = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        M = "%" + M;
                    }

                    if (elementLength >= 3)
                    {
                        info = new DateTimeInfo
                        {
                            IsReadOnly = false,
                            Type       = DateTimePart.MonthName,
                            Format     = M
                        }
                    }
                    ;
                    else
                    {
                        info = new DateTimeInfo
                        {
                            IsReadOnly = false,
                            Type       = DateTimePart.Month,
                            Format     = M
                        }
                    };
                    break;
                }

                case 'S':
                case 's':
                {
                    string s = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        s = "%" + s;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = false,
                        Type       = DateTimePart.Second,
                        Format     = s
                    };
                    break;
                }

                case 'T':
                case 't':
                {
                    string t = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        t = "%" + t;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = false,
                        Type       = DateTimePart.AmPmDesignator,
                        Format     = t
                    };
                    break;
                }

                case 'Y':
                case 'y':
                {
                    string y = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        y = "%" + y;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = false,
                        Type       = DateTimePart.Year,
                        Format     = y
                    };
                    break;
                }

                case '\\':
                {
                    if (format.Length >= 2)
                    {
                        info = new DateTimeInfo
                        {
                            IsReadOnly = true,
                            Content    = format.Substring(1, 1),
                            Length     = 1,
                            Type       = DateTimePart.Other
                        };
                        elementLength = 2;
                    }
                    break;
                }

                case 'g':
                {
                    string g = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        g = "%" + g;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = true,
                        Type       = DateTimePart.Period,
                        Format     = format.Substring(0, elementLength)
                    };
                    break;
                }

                case 'm':
                {
                    string m = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        m = "%" + m;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = false,
                        Type       = DateTimePart.Minute,
                        Format     = m
                    };
                    break;
                }

                case 'z':
                {
                    string z = format.Substring(0, elementLength);
                    if (elementLength == 1)
                    {
                        z = "%" + z;
                    }

                    info = new DateTimeInfo
                    {
                        IsReadOnly = true,
                        Type       = DateTimePart.TimeZone,
                        Format     = z
                    };
                    break;
                }

                default:
                {
                    elementLength = 1;
                    info          = new DateTimeInfo
                    {
                        IsReadOnly = true,
                        Length     = 1,
                        Content    = format[0].ToString(),
                        Type       = DateTimePart.Other
                    };
                    break;
                }
                }

                _dateTimeInfoList.Add(info);
                format = format.Substring(elementLength);
            }
        }
Ejemplo n.º 13
0
        private DateTime?UpdateDateTime(DateTime?currentDateTime, int value)
        {
            DateTimeInfo info = _selectedDateTimeInfo;

            //this only occurs when the user manually type in a value for the Value Property
            if (info == null)
            {
                info = (this.CurrentDateTimePart != DateTimePart.Other) ? this.GetDateTimeInfo(this.CurrentDateTimePart) : _dateTimeInfoList[0];
            }

            DateTime?result = null;

            try
            {
                switch (info.Type)
                {
                case DateTimePart.Year:
                {
                    result = (( DateTime )currentDateTime).AddYears(value);
                    break;
                }

                case DateTimePart.Month:
                case DateTimePart.MonthName:
                {
                    result = (( DateTime )currentDateTime).AddMonths(value);
                    break;
                }

                case DateTimePart.Day:
                case DateTimePart.DayName:
                {
                    result = (( DateTime )currentDateTime).AddDays(value);
                    break;
                }

                case DateTimePart.Hour12:
                case DateTimePart.Hour24:
                {
                    result = (( DateTime )currentDateTime).AddHours(value);
                    break;
                }

                case DateTimePart.Minute:
                {
                    result = (( DateTime )currentDateTime).AddMinutes(value);
                    break;
                }

                case DateTimePart.Second:
                {
                    result = (( DateTime )currentDateTime).AddSeconds(value);
                    break;
                }

                case DateTimePart.Millisecond:
                {
                    result = (( DateTime )currentDateTime).AddMilliseconds(value);
                    break;
                }

                case DateTimePart.AmPmDesignator:
                {
                    result = (( DateTime )currentDateTime).AddHours(value * 12);
                    break;
                }

                default:
                {
                    break;
                }
                }
            }
            catch
            {
                //this can occur if the date/time = 1/1/0001 12:00:00 AM which is the smallest date allowed.
                //I could write code that would validate the date each and everytime but I think that it would be more
                //efficient if I just handle the edge case and allow an exeption to occur and swallow it instead.
            }

            return(this.CoerceValueMinMax(result));
        }
Ejemplo n.º 14
0
        protected override void InitializeDateTimeInfoList()
        {
            DateTimeInfo dayInfo = _dateTimeInfoList.FirstOrDefault(x => x.Type == DateTimePart.Day);
            bool         hasDay  = dayInfo != null;

            _dateTimeInfoList.Clear();

            if (this.Value.HasValue && this.Value.Value.Days != 0)
            {
                int dayLength = this.Value.Value.Days.ToString().Length;
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Day, Length = dayLength, Format = "dd"
                });
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Other, Length = 1, Content = ".", IsReadOnly = true
                });

                // Day has been added, move TextBox.Selection to keep it on current DateTimeInfo
                if (!hasDay)
                {
                    this.TextBox.SelectionStart += (dayLength + 1);
                }
            }
            // Day has been removed, move TextBox.Selection to keep it on current DateTimeInfo
            else if (hasDay)
            {
                this.TextBox.SelectionStart = Math.Max(0, this.TextBox.SelectionStart - (dayInfo.Length + 1));
            }

            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Hour24, Length = 2, Format = "hh"
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Other, Length = 1, Content = ":", IsReadOnly = true
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Minute, Length = 2, Format = "mm"
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Other, Length = 1, Content = ":", IsReadOnly = true
            });
            _dateTimeInfoList.Add(new DateTimeInfo()
            {
                Type = DateTimePart.Second, Length = 2, Format = "ss"
            });

            if (this.Value.HasValue && this.Value.Value.Milliseconds != 0)
            {
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Other, Length = 1, Content = ".", IsReadOnly = true
                });
                _dateTimeInfoList.Add(new DateTimeInfo()
                {
                    Type = DateTimePart.Second, Length = 7, Format = "fffffff"
                });
            }

            if (this.Value.HasValue)
            {
                this.ParseValueIntoTimeSpanInfo();
            }
        }