TryParse() public static method

public static TryParse ( string value, string format, System.DateTime currentDate, CultureInfo cultureInfo, System.DateTime &result ) : bool
value string
format string
currentDate System.DateTime
cultureInfo System.Globalization.CultureInfo
result System.DateTime
return bool
コード例 #1
0
        private bool TryParseDateTime(string text, out DateTime result)
        {
            bool isValid = false;

            result = this.ContextNow;

            DateTime current = this.ContextNow;

            try
            {
                current = (this.Value.HasValue)
                    ? this.Value.Value
                    : DateTime.Parse(this.ContextNow.ToString(), this.CultureInfo.DateTimeFormat);

                isValid = DateTimeParser.TryParse(text, this.GetFormatString(Format), current, this.CultureInfo, out result);
            }
            catch (FormatException)
            {
                isValid = false;
            }

            if (!isValid)
            {
                isValid = DateTime.TryParseExact(text, this.GetFormatString(this.Format), this.CultureInfo, DateTimeStyles.None, out result);
            }

            if (!isValid)
            {
                result = (_lastValidDate != null) ? _lastValidDate.Value : current;
            }

            return(isValid);
        }
コード例 #2
0
        private bool TryParseDateTime(string text, out DateTime result)
        {
            bool isValid = false;

            result = this.ContextNow;

            DateTime current = this.ContextNow;

            try
            {
                // TempValue is used when Manipulating TextBox.Text while Value is not updated yet (used in DateTimePicker's TimePicker).
                current = this.TempValue.HasValue
                  ? this.TempValue.Value
                  : this.Value.HasValue ? this.Value.Value : DateTime.Parse(this.ContextNow.ToString(), this.CultureInfo.DateTimeFormat);

                isValid = DateTimeParser.TryParse(text, this.GetFormatString(Format), current, this.CultureInfo, this.AutoClipTimeParts, out result);
            }
            catch (FormatException)
            {
                isValid = false;
            }

            if (!isValid)
            {
                isValid = DateTime.TryParseExact(text, this.GetFormatString(this.Format), this.CultureInfo, DateTimeStyles.None, out result);
            }

            if (!isValid)
            {
                result = (_lastValidDate != null) ? _lastValidDate.Value : current;
            }

            return(isValid);
        }
コード例 #3
0
        protected override DateTime?ConvertTextToValue(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            DateTime current = Value.HasValue ? Value.Value : DateTime.Parse(DateTime.Now.ToString(), CultureInfo.DateTimeFormat);
            DateTime result;
            var      success = DateTimeParser.TryParse(text, GetFormatString(Format), current, CultureInfo, out result);

            return(result);
        }
コード例 #4
0
ファイル: DateTimeUpDown.cs プロジェクト: huangjia2107/gbox
        private bool TryParseDateTime(string text, out DateTime result)
        {
            bool isValid;

            DateTime current = this.Value.HasValue ? this.Value.Value : DateTime.Parse(DateTime.Now.ToString(), this.CultureInfo.DateTimeFormat);

            isValid = DateTimeParser.TryParse(text, this.GetFormatString(Format), current, this.CultureInfo, out result);

            if (!isValid && (this.Format == DateTimeFormat.Custom))
            {
                isValid = DateTime.TryParseExact(text, this.GetFormatString(this.Format), this.CultureInfo, DateTimeStyles.None, out result);
            }

            return(isValid);
        }
コード例 #5
0
        protected override void OnTextChanged(string previousValue, string currentValue)
        {
            if (!_processTextChanged)
            {
                return;
            }

            if (!String.IsNullOrEmpty(currentValue))
            {
                DateTime current = Value.HasValue ? Value.Value : DateTime.Parse(DateTime.Now.ToString(), CultureInfo.DateTimeFormat);
                DateTime result;
                var      success = DateTimeParser.TryParse(currentValue, GetFormatString(Format), current, CultureInfo, out result);
                currentValue = result.ToString();
            }

            SyncTextAndValueProperties(true, currentValue);
        }
コード例 #6
0
        protected override void OnTextChanged(string previousValue, string currentValue)
        {
            if (!_processTextChanged)
            {
                return;
            }

            //TODO: clean this up and make sure it doesn't fire recursively
            if (String.IsNullOrEmpty(currentValue))
            {
                Value = null;
                return;
            }

            DateTime current = Value.HasValue ? Value.Value : DateTime.Parse(DateTime.Now.ToString(), CultureInfo.DateTimeFormat);
            DateTime result;
            var      success = DateTimeParser.TryParse(currentValue, GetFormatString(Format), current, CultureInfo, out result);

            SyncTextAndValueProperties(InputBase.TextProperty, result.ToString(CultureInfo));
        }