MaskedEditBox percent with decimals

2 posts, 0 answers
  1. Jon Masters
    Jon Masters avatar
    35 posts
    Member since:
    Jan 2009

    Posted 25 Nov 2009 Link to this post

    My users are complaining about the fact they have to use the arrow keys to move over the decimal point.

    Is there a way to make the decimal character accepted by the maskededitbox?

    I realize there were some requests back in March, but it doesn't seem to have made it that I can find into the current build.

    Associated, would be nice if the solution also worked for countries that use a , instead of a . for the decimal.

  2. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 26 Nov 2009 Link to this post

    Hello Jon Masters,

    Unfortunately, RadMaskedEditBox does not support this feature. However I have prepared a workaround which you can use as a solution:

    1. Subscribe to the KeyUp event of the internal TextBox.
    2. Calculate the digitsSeparator position in the Text and move the textbox's caret to the calculated position.

    void HostedControl_KeyUp(object sender, KeyEventArgs e)
            {
                string value = this.radMaskedEditBox1.Text.ToString();
                string valueAfterParse = "";
                char digitsSeparator = '.';
                int selection = 0;

                for (int i = 0; i < value.Length; i++)
                {
                    if (Char.IsDigit(value[i]))
                    {
                        valueAfterParse += value[i];
                    }
                    else if (value[i] == digitsSeparator)
                    {
                        valueAfterParse += value[i];
                    }

                }

                bool shouldMoveCaret = false;

                if ((int)e.KeyValue == 190)
                {
                    selection = valueAfterParse.IndexOf(digitsSeparator);
                    shouldMoveCaret = true;
                }
                else
                {
                    if ((int)e.KeyValue == 189)
                    {
                        selection = valueAfterParse.IndexOf(digitsSeparator);
                        shouldMoveCaret = true;
                    }
                }

                if (!shouldMoveCaret)
                    return;

                string text = "";
                int caretPosition = 0;
                int selectionPosition = 0;

                for (int i = 0; i < this.radMaskedEditBox1.Text.Length; i++)
                {
                    if (Char.IsDigit(this.radMaskedEditBox1.Text[i]))
                    {
                        caretPosition++;
                        if (caretPosition >= selection)
                        {
                            selectionPosition = i-1;
                        }
                    }
                }

                if (selectionPosition >= 0)
                {
                    this.radMaskedEditBox1.SelectionStart = selectionPosition;
                }
            }

    3. Please note that the digitsSeparator variable specifies which is the current separator character. This character should be dependent on the Culture settings.

    If you need more information, please contact us again.


    Kind regards,
    Boyko Markov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top