예제 #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)
        {
            int position = SelectionStart;

            if (IsNullable && string.IsNullOrEmpty(this.Text))
            {
                this.Value = DateTime.Now;
            }

            if (!IsNullable && string.IsNullOrEmpty(this.Text))
            {
                throw new Exception("Only IsNullable is set to true, Text can be empty.");
            }

            MaskedTextProvider provider = MaskProvider;

            if (position < Text.Length)
            {
                position = GetNextCharacterPosition(position);
                //if (!Keyboard.IsKeyToggled(Key.Insert))
                //    PressKey(Key.Insert);
                //InputSimulator.SimulateKeyPress(VirtualKeyCode.INSERT);

                //if (Keyboard.IsKeyToggled(Key.Insert))
                {
                    if (e.Text == "0")
                    {
                        if (provider.Replace(e.Text, position))
                        {
                            position++;
                            position = GetNextCharacterPosition(position);
                            RefreshText(provider, position);
                        }
                    }
                    else
                    {
                        string result = FillValidDate(IsAnyInvalidPart(),
                                                      (Text.Substring(0, position) + e.Text + Text.Substring(position + 1)).ToString());
                        if (DateTimeEditor.formatProvider[CultureInfo.Name].regex.IsMatch(result))
                        {
                            if (provider.Replace(e.Text, position))
                            {
                                position++;
                                position = GetNextCharacterPosition(position);
                                RefreshText(provider, position);
                            }
                        }
                    }
                }
                //else
                //{
                //throw new Exception("Insert mode is broken.");
                //}
            }
            e.Handled = true;
            base.OnPreviewTextInput(e);
        }
        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            e.Handled = true;
            var validInput = false;

            if (!string.IsNullOrWhiteSpace(e.Text) && e.Text.Length == 1)
            {
                MaskedTextResultHint hint;
                validInput = MaskProvider.VerifyChar(e.Text[0], SelectionStart, out hint);
                if (validInput)
                {
                    var initialString = MaskProvider.ToDisplayString();
                    var charArray     = initialString.ToCharArray();
                    charArray[SelectionStart] = e.Text[0];
                    var verifyString = new string(charArray);

                    TimeSpan ts;
                    if (TimeSpan.TryParse(verifyString, CultureInfo.CurrentCulture, out ts))
                    {
                        MaskProvider.Replace(e.Text, SelectionStart);
                    }
                    else
                    {
                        // Case 08:00:00 => 28:00:00 => 20:00:00
                        if (SelectionStart + 1 < charArray.Length)
                        {
                            charArray[SelectionStart + 1] = '0';
                            verifyString = new string(charArray);
                            if (TimeSpan.TryParse(verifyString, CultureInfo.CurrentCulture, out ts))
                            {
                                MaskProvider.Replace(e.Text, SelectionStart);
                                MaskProvider.Replace('0', SelectionStart + 1);
                            }
                            else
                            {
                                validInput = false;
                            }
                        }
                        else
                        {
                            validInput = false;
                        }
                    }
                }
            }
            base.OnPreviewTextInput(e);
            if (validInput)
            {
                RefreshText();
                SelectionStart = GetNextCharacterPosition(SelectionStart);
            }
        }
        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);
        }
예제 #4
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;
        }
        public bool Clear(ref int position, int length, bool forward)
        {
            var oldReset = Provider.ResetOnPrompt;

            Provider.ResetOnPrompt = true;
            var pos = position;

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

            pos = Provider.FindEditPositionFrom(pos, forward);
            var count = EditPositions.Count(r => r >= pos && r <= endPos);

            var success = Provider.Replace(new String(Provider.PromptChar, count), pos, endPos, out testPosition, out resultHint);

            if (success)
            {
                position = pos;
            }
            Provider.ResetOnPrompt = oldReset;
            return(success);
        }
예제 #6
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);
            }
        }
예제 #7
0
        private void MaskedTextBoxPreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TreatSelectedText();

            int position = GetNextCharacterPosition(SelectionStart, true);

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

            position = GetNextCharacterPosition(position, true);

            RefreshText(position);

            e.Handled = true;
        }
예제 #8
0
        public void Replace_InputString_StartPosition_EndPosition_TestPosition_ResultHint()
        {
            var maskedTextProvider = new MaskedTextProvider(TestSimpleDigitMask);

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

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

            Assert.False(maskedTextProvider.Replace("2", -1, 5, out testPosition, out resultHint));
            Assert.Equal(MaskedTextResultHint.PositionOutOfRange, resultHint);

            Assert.True(maskedTextProvider.Replace("2", 0, 5, out testPosition, out resultHint));
            Assert.Equal("2-__-_", maskedTextProvider.ToDisplayString());
            Assert.Equal(0, testPosition);
            Assert.Equal(MaskedTextResultHint.Success, resultHint);
        }
예제 #9
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());
        }
예제 #10
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);
        }
예제 #11
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);
        }
예제 #12
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);
        }
예제 #13
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;
        }
        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);
        }
예제 #15
0
        private void CreateProvider()
        {
            string text = _target.Text;

            if (_provider != null)
            {
                text = _provider.ToString(false, false);
            }

            _provider = new MaskedTextProvider(
                GetMask(_target),
                CultureInfo.CurrentCulture,
                true, // allow prompt as input
                GetPromptChar(_target),
                '\0',
                false); // Ascii only

            int testPosition;
            MaskedTextResultHint hint;

            _provider.Replace(text, 0, _provider.Length, out testPosition, out hint);
            SetText();
        }
예제 #16
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);
        }
예제 #17
0
 ///<inheritdoc/>
 public bool Delete(int pos)
 {
     return(provider.Replace(' ', pos));                // .RemoveAt (pos);
 }
예제 #18
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)
        {
            //eat the keystroke if insert is pressed
            //if (!Keyboard.IsKeyToggled(Key.Insert))
            //{
            //InputSimulator.SimulateKeyPress(VirtualKeyCode.INSERT);
            //    PressKey(Key.Insert);
            //}
            if (e.Key == Key.Tab)
            {
                return;
            }

            MaskedTextProvider provider = MaskProvider;
            int position = SelectionStart;

            if (e.Key == Key.Delete && position < Text.Length)//handle the delete key
            {
                int tmp = GetNextCharacterPosition(position);
                if (provider.Replace(MaskProvider.PromptChar, tmp))
                {
                    RefreshText(provider, ++tmp);
                }

                e.Handled = true;
            }

            else if (e.Key == Key.Back)//handle the back space
            {
                if (position > 0)
                {
                    position = GetPreivousCharacterPosition(position);
                    if (provider.Replace(MaskProvider.PromptChar, position))
                    {
                        RefreshText(provider, position);
                    }
                }
                if (position == 0 && IsNullable)
                {
                    Text = "";
                }
                e.Handled = true;
            }

            else if (e.Key == Key.Left)
            {
                if (position > 0)
                {
                    position       = GetPreivousCharacterPosition(position);
                    SelectionStart = position;
                }
                e.Handled = true;
            }

            else if (e.Key == Key.Space || e.Key == Key.Insert)
            {
                e.Handled = true;
            }

            else if (e.Key == Key.Right)
            {
                if (position >= 0)
                {
                    position       = GetNextCharacterPosition(++position);
                    SelectionStart = position;
                }
                e.Handled = true;
            }
            else if (e.Key == Key.Tab)
            {
                e.Handled = false;
            }
            base.OnPreviewKeyDown(e);
        }