public void DeleteText(int start, int end)
        {
            if (maskedTextBox.ReadOnly)
            {
                throw new ElementNotEnabledException();
            }

            if (Provider.Control.InvokeRequired)
            {
                Provider.Control.BeginInvoke(new DeleteTextDelegate(DeleteText),
                                             new object [] { start, end });
                return;
            }

            // MaskedTextProvider.RemoveAt does not work for this.
            System.ComponentModel.MaskedTextProvider prov
                = maskedTextBox.MaskedTextProvider;

            string text = prov.ToDisplayString();

            for (int i = start; i < end; i++)
            {
                if (!prov.IsEditPosition(i) ||
                    prov.IsAvailablePosition(i))
                {
                    continue;
                }

                text = text.Substring(0, i) + prov.PromptChar
                       + text.Substring(i + 1);
            }

            maskedTextBox.Text = text;
        }
Example #2
0
        /// <summary>
        /// When text is received by the TextBox we check whether to accept it or not
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            string PreviousText = this.Text;

            if (NewTextIsOk)
            {
                base.OnTextInput(e);
                if (_mprovider.VerifyString(this.Text) == false)
                {
                    this.Text = PreviousText;
                }
                while (!_mprovider.IsEditPosition(this.CaretIndex) && _mprovider.Length > this.CaretIndex)
                {
                    this.CaretIndex++;
                }
            }
            else
            {
                e.Handled = true;
            }
        }
Example #3
0
        /// <summary>
        /// When text is received by the TextBox we check whether to accept it or not
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            string PreviousText = this.Text;

            if (NewTextIsOk)
            {
                base.OnTextInput(e);
                switch (_maskType)
                {
                case MaskType.Mask:
                {
                    if (_mprovider.VerifyString(this.Text) == false)
                    {
                        this.Text = PreviousText;
                    }
                    while (!_mprovider.IsEditPosition(this.CaretIndex) && _mprovider.Length > this.CaretIndex)
                    {
                        this.CaretIndex++;
                    }
                }
                break;

                case MaskType.DoubleNumber:
                {
                    double num = 0;
                    if (double.TryParse(Text + "0", System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out num) == false)
                    {
                        this.Text = PreviousText;
                    }
                }
                break;

                case MaskType.IntegerNumber:
                {
                    int  num = 0;
                    bool res = true;
                    switch (bus)
                    {
                    case EnumerationSystem.Dec:
                        res = int.TryParse(Text, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out num);
                        break;

                    case EnumerationSystem.Bin:
                    {
                        foreach (char ch in Text)
                        {
                            if (!((ch == '0') || (ch == '1')))
                            {
                                res = false;
                                break;
                            }
                        }
                    }
                    break;

                    case EnumerationSystem.Oct:
                    {
                        this._NewTextIsOk = true;
                        foreach (char ch in Text)
                        {
                            if (!((ch == '0') || (ch == '1') || (ch == '2') || (ch == '3') || (ch == '4') || (ch == '5') || (ch == '6') || (ch == '7')))
                            {
                                res = false;
                                break;
                            }
                        }
                    }
                    break;

                    case EnumerationSystem.Hex:
                        res = int.TryParse(Text, System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out num);
                        break;
                    }
                    if (res == false)
                    {
                        this.Text = PreviousText;
                    }
                }
                break;
                }
            }
            else
            {
                e.Handled = true;
            }
        }
        // This is kind of crack.  Basically, in order to emulate what
        // happens in the GUI, InsertText will actually overwrite
        // characters in the DisplayString as long as they're
        // placeholders.  If we just inserted as usual, it would be
        // rejected as it contains too many characters.
        public void InsertText(string str, ref int position)
        {
            if (maskedTextBox.ReadOnly)
            {
                throw new ElementNotEnabledException();
            }

            if (Provider.Control.InvokeRequired)
            {
                object[] args = new object[] { str, position };
                Provider.Control.BeginInvoke(new InsertTextDelegate(InsertText), args);
                position = (int)args[1];
                return;
            }

            System.ComponentModel.MaskedTextProvider prov
                = maskedTextBox.MaskedTextProvider;

            string text = prov.ToDisplayString();

            int offset = position;

            for (int i = 0; i < str.Length; i++)
            {
                int localOffset = offset;

                // skip over an unavailable slot if we see one
                bool reachedBounds = false;
                while (!prov.IsEditPosition(i + localOffset))
                {
                    localOffset++;
                    if (i + localOffset >= prov.Length)
                    {
                        reachedBounds = true;
                        break;
                    }
                }

                if (reachedBounds)
                {
                    break;
                }

                // There is already a character here, so while
                // the GUI will actually move this character
                // over, we cannot as there are _many, many_
                // edge cases that we cannot handle.
                // Instead, we skip over the character.
                if (!prov.IsAvailablePosition(i + localOffset))
                {
                    // Make sure offset is correct for the
                    // next iteration
                    offset = localOffset;
                    continue;
                }

                // The correct value of position is equal to
                // the localOffset of the first character.
                //
                // This check needs to be done after
                // IsAvailablePosition in case we are trying to
                // insert into a place that already has a
                // character.
                if (offset != localOffset && i == 0)
                {
                    position = localOffset;
                }

                offset = localOffset;

                text = text.Substring(0, i + offset)
                       + str[i] + text.Substring(i + offset + 1);
            }

            maskedTextBox.Text = text;
        }