This is a migrated thread and some comments may be shown as answers.

MaskedEditBox percent with decimals

1 Answer 132 Views
MaskedEditBox
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 26 Nov 2009, 04:06 AM
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.

1 Answer, 1 is accepted

Sort by
0
Boyko Markov
Telerik team
answered on 26 Nov 2009, 12:34 PM
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.
Tags
MaskedEditBox
Asked by
Jon
Top achievements
Rank 1
Answers by
Boyko Markov
Telerik team
Share this question
or