private void RemoveText(int position, int length)
        {
            if (length == 0)
            {
                return;
            }

            MaskProvider.RemoveAt(position, position + length - 1);
        }
Beispiel #2
0
        //gets the next position in the textbox to move
        private int GetNextCharacterPosition(int startPosition)
        {
            int position = MaskProvider.FindEditPositionFrom(startPosition, true);

            if (position == -1)
            {
                return(startPosition);
            }

            return(position);
        }
        private bool RemoveSelectedText()
        {
            int length = SelectionLength;

            if (length == 0)
            {
                return(false);
            }

            int position = SelectionStart;

            return(MaskProvider.RemoveAt(position, position + length - 1));
        }
        //gets the next position in the textbox to move
        private int GetPreivousCharacterPosition(int startPosition)
        {
            int position = -1;

            for (int i = startPosition - 1; i >= 0; i--)
            {
                position = MaskProvider.FindEditPositionFrom(i, true);
                if (position < startPosition && position != -1)
                {
                    return(position);
                }
            }
            return(startPosition);
        }
Beispiel #5
0
        private void DecreaseMask(int position, int length)
        {
            var decPos = GetDecimalSeparatorPosition(Mask);
            var str    = MaskProvider.ToDisplayString();

            if (position > decPos)
            {
                Text       = str.Remove(position, length).Insert(position, 0.ToString("D" + length));
                CaretIndex = position;
                return;
            }

            if (position + length > decPos)
            {
                var decLength   = position + length - decPos - 1;
                var decPosition = decPos + 1;
                str = str.Remove(decPosition, decLength).Insert(decPosition, 0.ToString("D" + decLength));

                length = decPos - position;
            }

            if (length > 0)
            {
                str = str.Remove(position, length).Replace(" ", string.Empty);
                if (str.Length == 0)
                {
                    str = PromptChar.ToString();
                }

                var mask = FormatMask(Mask.Remove(position, length));
                if (mask.Length == 0 || mask.StartsWith("."))
                {
                    if (mask.StartsWith("."))
                    {
                        str = PromptChar + str;
                    }

                    mask = PromptChar + mask;
                }

                Mask = mask;

                Text = str;
            }

            CaretIndex = position;
        }
        private object ConvertTextToValue(string text)
        {
            object convertedValue = null;

            Type dataType = ValueType;

            string valueToConvert = MaskProvider.ToString().Trim();

            try
            {
                if (valueToConvert.GetType() == dataType || dataType.IsInstanceOfType(valueToConvert))
                {
                    convertedValue = valueToConvert;
                }
//#if VS2008
//        else if( String.IsNullOrWhiteSpace( valueToConvert ) )
//        {
//          convertedValue = Activator.CreateInstance( dataType );
//        }
//#else
//        else if ( String.IsNullOrEmpty( valueToConvert ) )
//        {
//          convertedValue = Activator.CreateInstance( dataType );
//        }
//#endif
                else if (String.IsNullOrEmpty(valueToConvert))
                {
                    convertedValue = Activator.CreateInstance(dataType);
                }
                else if (null == convertedValue && valueToConvert is IConvertible)
                {
                    convertedValue = Convert.ChangeType(valueToConvert, dataType);
                }
            }
            catch
            {
                //if an excpetion occurs revert back to original value
                _convertExceptionOccurred = true;
                return(Value);
            }

            return(convertedValue);
        }
        private string ConvertValueToText(object value)
        {
            if (value == null)
            {
                value = string.Empty;
            }

            if (_convertExceptionOccurred)
            {
                value = Value;
                _convertExceptionOccurred = false;
            }

            //I have only seen this occur while in Blend, but we need it here so the Blend designer doesn't crash.
            if (MaskProvider == null)
            {
                return(value.ToString());
            }

            MaskProvider.Set(value.ToString());
            return(MaskProvider.ToDisplayString());
        }
        private void Paste(object sender, RoutedEventArgs e)
        {
            if (IsReadOnly)
            {
                return;
            }

            object data = Clipboard.GetData(DataFormats.Text);

            if (data != null)
            {
                string text = data.ToString().Trim();
                if (text.Length > 0)
                {
                    int position = SelectionStart;

                    MaskProvider.Set(text);

                    UpdateText(position);
                }
            }
        }