예제 #1
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;
            }
        }
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            // If the control is in ReadOnly mode, we don't want
            // to accept key input
            if (this.IsReadOnly == true)
            {
                e.Handled = false;
                return;
            }

            base.OnKeyDown(e);

            MaskedTextProvider maskProvider = GetMaskProvider();
            int pos = this.SelectionStart;

            // Deleting a character (Delete key).
            // Currently this does nothing if you try to delete
            // a format character (unliked MaskedTextBox, which
            // deletes the next input character).
            // Could use our private SkipToEditableCharacter
            // method to change this behavior.
            if (e.Key == Key.Delete && pos < (this.Text.Length))
            {
                if (maskProvider.RemoveAt(pos))
                {
                    RefreshText(maskProvider, pos);
                }
                e.Handled = true;
            }

            // 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.
            else if (e.Key == Key.Back)
            {
                if (pos > 0)
                {
                    pos--;
                    if (maskProvider.RemoveAt(pos))
                    {
                        RefreshText(maskProvider, pos);
                    }
                }
                e.Handled = true;
            }
        }
        public bool Delete(ref int position, int length, bool forward)
        {
            if (!forward && position == 0)
            {
                return(false);
            }
            var pos = position;

            if (!forward)
            {
                pos--;
            }
            var endPos = pos + length - 1;

            pos = Provider.FindEditPositionFrom(pos, forward);
            if (pos >= 0 && Provider.RemoveAt(pos, endPos))
            {
                position = pos;
                if (!SkipLiterals)
                {
                    position = Provider.FindEditPositionFrom(pos, forward);
                }
                return(true);
            }
            return(false);
        }
예제 #4
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);
        }
예제 #5
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);
        }
예제 #6
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            int pos = this.SelectionStart;

            // Deleting a character (Delete key).
            // Currently this does nothing if you try to delete
            // a format character (unliked MaskedTextBox, which
            // deletes the next input character).
            // Could use our private SkipToEditableCharacter
            // method to change this behavior.
            if ((int)e.KeyCode == (int)Keys.Delete && pos < (this.Text.Length))
            {
                if (maskProvider.RemoveAt(pos))
                {
                    RefreshText(pos);
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }
예제 #7
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;
            }
        }
예제 #8
0
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);

            MaskedTextProvider maskProvider = GetMaskProvider();
            int pos = this.SelectionStart;

            // Deleting a character (Delete key).
            // Currently this does nothing if you try to delete a format character
            // (unliked MaskedTextBox, which deletes the next input character).
            // Could use our private SkipToEditableCharacter method to change this behavior.
            if ((e.Key == Key.Delete) && (pos < this.Text.Length))
            {
                if (maskProvider.RemoveAt(pos))
                {
                    RefreshText(maskProvider, pos);
                }
                e.Handled = true;
            }

            // 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.
            else if (e.Key == Key.Back)
            {
                if (pos > 0)
                {
                    pos--;
                    if (maskProvider.RemoveAt(pos))
                    {
                        RefreshText(maskProvider, pos);
                    }
                }
                e.Handled = true;
            }
        }
예제 #9
0
        private static bool CanReplace(MaskedTextProvider provider, string text, int startPosition, int selectionLength, bool rejectInputOnFirstFailure, out int tentativeCaretIndex)
        {
            var endPosition = startPosition + selectionLength - 1;

            tentativeCaretIndex = -1;

            var success = false;

            foreach (var character in text)
            {
                if (!provider.VerifyEscapeChar(character, startPosition))
                {
                    var editPositionFrom = provider.FindEditPositionFrom(startPosition, true);
                    if (editPositionFrom == MaskedTextProvider.InvalidIndex)
                    {
                        break;
                    }
                    startPosition = editPositionFrom;
                }

                var length    = endPosition >= startPosition ? 1 : 0;
                var overwrite = length > 0;

                if (PlaceChar(provider, character, startPosition, length, overwrite, out tentativeCaretIndex))
                {
                    // Only one successfully inserted character is enough to declare the replace operation successful.
                    success       = true;
                    startPosition = tentativeCaretIndex + 1;
                }
                else if (rejectInputOnFirstFailure)
                {
                    return(false);
                }
            }

            if ((selectionLength > 0) && (startPosition <= endPosition))
            {
                // Erase the remaining of the assigned edit character.
                int notUsed;
                MaskedTextResultHint notUsedHint;
                if (!provider.RemoveAt(startPosition, endPosition, out notUsed, out notUsedHint))
                {
                    success = false;
                }
            }

            return(success);
        }
        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);
        }
예제 #11
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);
        }