Пример #1
0
        /// <summary>
        ///     override this method to replace the characters enetered with the mask
        /// </summary>
        /// <param name="e">Arguments for event</param>
        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            var position = SelectionStart;

            var length = Text.Length;

            if (position < length || length == 0)
            {
                position = FindCharacterPosition(position);

                if (Keyboard.IsKeyToggled(Key.Insert))
                {
                    if (_maskedTextProvider.Replace(e.Text, position))
                    {
                        position++;
                    }
                }
                else
                {
                    if (_maskedTextProvider.InsertAt(e.Text, position))
                    {
                        position++;
                    }
                }

                position = FindCharacterPosition(position);
            }

            Refresh(position);
            e.Handled = true;
        }
Пример #2
0
        public void InsertAt_InputString_Position()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.False(maskedTextProvider.InsertAt("1", -1));
            Assert.False(maskedTextProvider.InsertAt("1", 6));
            Assert.True(maskedTextProvider.InsertAt("1", 0));
        }
Пример #3
0
        private void MaskedTextBoxPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                TreatSelectedText();

                int position = GetNextCharacterPosition(SelectionStart, true);

                if (_provider.InsertAt(" ", position))
                {
                    RefreshText(position);
                }

                e.Handled = true;
            }

            if (e.Key == Key.Back)
            {
                TreatSelectedText();
                var position = this.GetNextCharacterPosition(SelectionStart, false);
                e.Handled = true;

                if (SelectionStart > 0)
                {
                    if (position == SelectionStart)
                    {
                        position = this.GetNextCharacterPosition(position - 1, false);
                    }
                    if (_provider.RemoveAt(position))
                    {
                        position = GetNextCharacterPosition(position, false);
                    }
                }

                RefreshText(position);

                e.Handled = true;
            }

            if (e.Key == Key.Delete)
            {
                if (TreatSelectedText())
                {
                    RefreshText(SelectionStart);
                }
                else
                {
                    if (_provider.RemoveAt(SelectionStart))
                    {
                        RefreshText(SelectionStart);
                    }
                }

                e.Handled = true;
            }
        }
Пример #4
0
        public void InsertAt_Input_Position_TestPosition_ResultHint()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt(string.Empty, 0, out int testPosition, out var resultHint));
            Assert.Equal(MaskedTextResultHint.NoEffect, resultHint);

            Assert.True(maskedTextProvider.InsertAt('1', 0, out testPosition, out resultHint));
            Assert.Equal(0, testPosition);
            Assert.Equal(MaskedTextResultHint.Success, resultHint);

            Assert.False(maskedTextProvider.InsertAt('1', 6, out testPosition, out resultHint));
            Assert.Equal(6, testPosition);
            Assert.Equal(MaskedTextResultHint.PositionOutOfRange, resultHint);
        }
Пример #5
0
        private void ReplaceText(string text)
        {
            int start = _target.SelectionStart;
            int end   = start + _target.SelectionLength - 1;

            int testPosition;
            MaskedTextResultHint hint;

            if (end >= start)
            {
                _provider.Replace(text, start, end, out testPosition, out hint);
            }
            else
            {
                _provider.InsertAt(text, start, out testPosition, out hint);
            }

            if (hint == MaskedTextResultHint.Success ||
                hint == MaskedTextResultHint.CharacterEscaped ||
                hint == MaskedTextResultHint.NoEffect ||
                hint == MaskedTextResultHint.SideEffect)
            {
                SetText();
                _target.SelectionStart  = testPosition + text.Length;
                _target.SelectionLength = 0;
            }
            else
            {
                //RaiseMaskInputRejectedEvent(hint, start);
            }
        }
        private void InsertText(string text)
        {
            int position = SelectionStart;
            MaskedTextProvider provider = MaskProvider;

            bool textRemoved = this.RemoveSelectedText();

            position = GetNextCharacterPosition(position);

            if (!textRemoved && Keyboard.IsKeyToggled(Key.Insert))
            {
                if (provider.Replace(text, position))
                {
                    position += text.Length;
                }
            }
            else
            {
                if (provider.InsertAt(text, position))
                {
                    position += text.Length;
                }
            }

            position = GetNextCharacterPosition(position);

            this.UpdateText(position);
        }
Пример #7
0
        public void Replace_InputString_Position()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt("12", 0));
            Assert.True(maskedTextProvider.Replace("23", 0));
            Assert.Equal(TestMaskResultReplaceString, maskedTextProvider.ToDisplayString());
        }
Пример #8
0
        public void Remove()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt("1", 0));
            Assert.Equal(1, maskedTextProvider.AssignedEditPositionCount);
            Assert.True(maskedTextProvider.Remove());
            Assert.Equal(0, maskedTextProvider.AssignedEditPositionCount);
        }
Пример #9
0
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (is_empty_mask)
            {
                base.OnKeyPress(e);
                return;
            }

            int testPosition, editPosition;
            MaskedTextResultHint resultHint;
            bool result;

            if (e.KeyChar == '\b')
            {
                if (SelectionLength == 0)
                {
                    result = provider.RemoveAt(SelectionStart - 1, SelectionStart - 1, out testPosition, out resultHint);
                }
                else
                {
                    result = provider.RemoveAt(SelectionStart, SelectionStart + SelectionLength - 1, out testPosition, out resultHint);
                }

                editPosition = testPosition;
            }
            else if (IsOverwriteMode || SelectionLength > 0)                 // Replace
            {
                int start = provider.FindEditPositionFrom(SelectionStart, true);
                int end   = SelectionLength > 0 ? SelectionStart + SelectionLength - 1 : start;
                result = provider.Replace(e.KeyChar, start, end, out testPosition, out resultHint);

                editPosition = testPosition + 1;
            }
            else
            {
                // Move chars to the right
                result       = provider.InsertAt(e.KeyChar, SelectionStart, out testPosition, out resultHint);
                editPosition = testPosition + 1;
            }

            PostprocessKeyboardInput(result, editPosition, testPosition, resultHint);

            e.Handled = true;
        }
Пример #10
0
        public void RemoveAt_StartPosition_EndPosition()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt("12", 0));
            Assert.Equal(2, maskedTextProvider.AssignedEditPositionCount);

            Assert.True(maskedTextProvider.RemoveAt(0, 2));
            Assert.Equal(0, maskedTextProvider.AssignedEditPositionCount);
        }
Пример #11
0
        /// <summary>
        /// override the key down to handle delete of a character
        /// </summary>
        /// <param name="e">Arguments for the event</param>
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            MaskedTextProvider provider = MaskProvider;
            int position        = SelectionStart;
            int selectionlength = SelectionLength;
            // If no selection use the start position else use end position
            int endposition = (selectionlength == 0) ? position : position + selectionlength - 1;

            if (e.Key == Key.Delete && position < Text.Length)//handle the delete key
            {
                if (provider.RemoveAt(position, endposition))
                {
                    RefreshText(provider, position);
                }

                e.Handled = true;
            }

            else if (e.Key == Key.Space)
            {
                if (provider.InsertAt(" ", position))
                {
                    RefreshText(provider, position);
                }
                e.Handled = true;
            }

            else if (e.Key == Key.Back)//handle the back space
            {
                if ((position > 0) && (selectionlength == 0))
                {
                    position--;
                    if (provider.RemoveAt(position))
                    {
                        RefreshText(provider, position);
                    }
                }

                if (selectionlength != 0)
                {
                    if (provider.RemoveAt(position, endposition))
                    {
                        if (position > 0)
                        {
                            position--;
                        }

                        RefreshText(provider, position);
                    }
                }

                e.Handled = true;
            }
        }
Пример #12
0
        public void Replace_InputChar_StartPosition_EndPosition_TestPosition_ResultHint()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt("12", 0));

            Assert.True(maskedTextProvider.Replace('2', 0, 5, out int testPosition, out MaskedTextResultHint resultHint));
            Assert.Equal("2-__-_", maskedTextProvider.ToDisplayString());
            Assert.Equal(0, testPosition);
            Assert.Equal(MaskedTextResultHint.Success, resultHint);
        }
Пример #13
0
        public void Remove_TestPosition_ResultHint()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt("1", 0));
            Assert.Equal(1, maskedTextProvider.AssignedEditPositionCount);

            Assert.True(maskedTextProvider.Remove(out var testPosition, out MaskedTextResultHint resultHint));
            Assert.Equal(MaskedTextResultHint.Success, resultHint);
            Assert.Equal(0, testPosition);
        }
Пример #14
0
        public void Replace_InputString_Position_TestPosition_ResultHint()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt("12", 0));

            Assert.True(maskedTextProvider.Replace("23", 0, out int testPosition, out MaskedTextResultHint resultHint));
            Assert.Equal(TestMaskResultReplaceString, maskedTextProvider.ToDisplayString());
            Assert.Equal(2, testPosition);
            Assert.Equal(MaskedTextResultHint.Success, resultHint);
        }
Пример #15
0
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (maskProvider != null)
            {
                int pos = this.SelectionStart;

                // Deleting a character (backspace).
                // Currently this steps over a format character
                // (unliked MaskedTextBox, which steps over and
                // deletes the next input character).
                // Could use our private SkipToEditableCharacter
                // method to change this behavior.
                if ((int)e.KeyChar == (int)Keys.Back)
                {
                    if (pos > 0)
                    {
                        pos--;
                        maskProvider.RemoveAt(pos);
                    }
                }
                // Adding a character.
                else if (pos < this.Text.Length)
                {
                    pos = SkipToEditableCharacter(pos);

                    // Overwrite mode is on.
                    if (GetKeyState((int)Keys.Insert) == 1)
                    {
                        if (maskProvider.Replace(e.KeyChar, pos))
                        {
                            pos++;
                        }
                    }
                    // Insert mode is on.
                    else
                    {
                        if (maskProvider.InsertAt(e.KeyChar, pos))
                        {
                            pos++;
                        }
                    }

                    // Find the new cursor position.
                    pos = SkipToEditableCharacter(pos);
                }
                RefreshText(pos);
                e.Handled = true;
            }

            base.OnKeyPress(e);
        }
        public bool Insert(char character, ref int position)
        {
            int testPosition;
            MaskedTextResultHint resultHint;

            if (Provider.InsertAt(character, position, out testPosition, out resultHint))
            {
                if (testPosition >= 0)
                {
                    position = Math.Min(Provider.Length, testPosition + 1);
                    if (AutoAdvance)
                    {
                        position = AdvanceToNextEditPosition(position);
                    }
                }
                return(true);
            }
            return(false);
        }
Пример #17
0
        public void RemoveAt_StartPosition_EndPosition_TestPosition_ResultHint()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

            Assert.True(maskedTextProvider.InsertAt("12", 0));
            Assert.Equal(2, maskedTextProvider.AssignedEditPositionCount);

            Assert.False(maskedTextProvider.RemoveAt(0, 6, out int testPosition, out MaskedTextResultHint resultHint));
            Assert.Equal(6, testPosition);
            Assert.Equal(MaskedTextResultHint.PositionOutOfRange, resultHint);

            Assert.False(maskedTextProvider.RemoveAt(-1, 4, out testPosition, out resultHint));
            Assert.Equal(-1, testPosition);
            Assert.Equal(MaskedTextResultHint.PositionOutOfRange, resultHint);

            Assert.True(maskedTextProvider.RemoveAt(0, 2, out testPosition, out resultHint));
            Assert.Equal(0, maskedTextProvider.AssignedEditPositionCount);
            Assert.Equal(0, testPosition);
            Assert.Equal(MaskedTextResultHint.Success, resultHint);
        }
        void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            //if the text is readonly do not add the text
            if (IsReadOnly)
            {
                e.Handled = true;
                return;
            }

            int position = SelectionStart;
            MaskedTextProvider provider = MaskProvider;

            if (position < Text.Length)
            {
                position = GetNextCharacterPosition(position);

                if (Keyboard.IsKeyToggled(Key.Insert))
                {
                    if (provider.Replace(e.Text, position))
                    {
                        position++;
                    }
                }
                else
                {
                    if (provider.InsertAt(e.Text, position))
                    {
                        position++;
                    }
                }

                position = GetNextCharacterPosition(position);
            }

            UpdateText(provider, position);
            e.Handled = true;

            base.OnPreviewTextInput(e);
        }
Пример #19
0
        /// <summary>
        /// override the key down to handle delete of a character
        /// </summary>
        /// <param name="e">Arguments for the event</param>
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            MaskedTextProvider provider = MaskProvider;
            int position = SelectionStart;

            if (e.Key == Key.Delete && position < Text.Length)//handle the delete key
            {
                if (provider.RemoveAt(position))
                {
                    RefreshText(provider, position);
                }

                e.Handled = true;
            }

            else if (e.Key == Key.Space)
            {
                if (provider.InsertAt(" ", position))
                {
                    RefreshText(provider, position);
                }
                e.Handled = true;
            }

            else if (e.Key == Key.Back)//handle the back space
            {
                if (position > 0)
                {
                    position--;
                    if (provider.RemoveAt(position))
                    {
                        RefreshText(provider, position);
                    }
                }
                e.Handled = true;
            }
        }
Пример #20
0
        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            MaskedTextProvider maskProvider = GetMaskProvider();
            int pos = this.SelectionStart;


            // Adding a character.
            if (pos < this.Text.Length)
            {
                pos = SkipToEditableCharacter(pos);

                // Overwrite mode is on.
                if (Keyboard.IsKeyToggled(Key.Insert))
                {
                    if (maskProvider.Replace(e.Text, pos))
                    {
                        pos++;
                    }
                }
                // Insert mode is on.
                else
                {
                    if (maskProvider.InsertAt(e.Text, pos))
                    {
                        pos++;
                    }
                }

                // Find the new cursor position.
                pos = SkipToEditableCharacter(pos);
            }
            RefreshText(maskProvider, pos);
            e.Handled = true;


            base.OnPreviewTextInput(e);
        }
Пример #21
0
        private static bool PlaceChar(MaskedTextProvider provider, char ch, int startPosition, int length, bool overwrite, out int caretPosition)
        {
            caretPosition = startPosition;

            if (startPosition < provider.Length)
            {
                MaskedTextResultHint notUsed;

                if (length > 0)
                {
                    var endPosition = startPosition + length - 1;
                    return(provider.Replace(ch, startPosition, endPosition, out caretPosition, out notUsed));
                }

                if (overwrite)
                {
                    return(provider.Replace(ch, startPosition, out caretPosition, out notUsed));
                }

                return(provider.InsertAt(ch, startPosition, out caretPosition, out notUsed));
            }

            return(false);
        }
Пример #22
0
        void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (IsReadOnly)
            {
                return;
            }

            MaskedTextProvider provider = MaskProvider;
            int position        = SelectionStart;
            int selectionlength = SelectionLength;
            // If no selection use the start position else use end position
            int endposition = (selectionlength == 0) ? position : position + selectionlength - 1;

            if (e.Key == Key.Delete && position < Text.Length)//handle the delete key
            {
                if (provider.RemoveAt(position, endposition))
                {
                    UpdateText(provider, position);
                }

                e.Handled = true;
            }
            else if (e.Key == Key.Space)
            {
                if (provider.InsertAt(" ", position))
                {
                    UpdateText(provider, position);
                }
                e.Handled = true;
            }
            else if (e.Key == Key.Back)//handle the back space
            {
                if ((position > 0) && (selectionlength == 0))
                {
                    position--;
                    if (provider.RemoveAt(position))
                    {
                        UpdateText(provider, position);
                    }
                }

                if (selectionlength != 0)
                {
                    if (provider.RemoveAt(position, endposition))
                    {
                        if (position > 0)
                        {
                            position--;
                        }

                        UpdateText(provider, position);
                    }
                }

                e.Handled = true;
            }

            //if all text is selected and the user begins to type, we want to delete all selected text and continue typing the new values
            if (SelectionLength == Text.Length)
            {
                if (provider.RemoveAt(position, endposition))
                {
                    UpdateText(provider, position);
                }
            }

            base.OnPreviewKeyDown(e);
        }
        void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (IsReadOnly)
            {
                return;
            }

            MaskedTextProvider provider = MaskProvider;
            int position        = SelectionStart;
            int selectionlength = SelectionLength;
            // If no selection use the start position else use end position
            int endposition = (selectionlength == 0) ? position : position + selectionlength - 1;

            if (e.Key == Key.Delete && position < Text.Length)//handle the delete key
            {
                if (provider.RemoveAt(position, endposition))
                {
                    UpdateText(provider, position);
                }

                e.Handled = true;
            }
            else if (e.Key == Key.Space)
            {
                if (provider.InsertAt(" ", position))
                {
                    UpdateText(provider, ++position);
                }
                e.Handled = true;
            }
            else if (e.Key == Key.Back)//handle the back space
            {
                if ((position > 0) && (selectionlength == 0))
                {
                    position--;
                    if (provider.RemoveAt(position))
                    {
                        UpdateText(provider, position);
                    }
                }

                if (selectionlength != 0)
                {
                    if (provider.RemoveAt(position, endposition))
                    {
                        if (position > 0)
                        {
                            position--;
                        }

                        UpdateText(provider, position);
                    }
                }

                e.Handled = true;
            }

            //if all text is selected and the user begins to type, we want to delete all selected text and continue typing the new values
            //but only if the user is not tabbing / shift tabbing or copying/pasting
            if (SelectionLength == Text.Length && (e.Key != Key.Tab && e.Key != Key.LeftShift && e.Key != Key.RightShift && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Up && e.Key != Key.Down &&
                                                   (Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.Control))
            {
                if (provider.RemoveAt(position, endposition))
                {
                    UpdateText(provider, position);
                }
            }

            base.OnPreviewKeyDown(e);
        }