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

When decimal . is pressed change decimal spot

6 Answers 129 Views
MaskedInput (Numeric, DateTime, Text, Currency)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
danparker276
Top achievements
Rank 2
danparker276 asked on 13 Apr 2011, 03:28 AM
My mask is #2.2
If I have a current value as 10.55.  Then I put my cursor between the 1 and the 0.  When I press the '.'  I want the number to change to 1.055 (or if it's #2.2   1.06 or 1.05).  Is there a way to do this?  Right now it just skips past the 0 and keeps the 10.

6 Answers, 1 is accepted

Sort by
0
danparker276
Top achievements
Rank 2
answered on 13 Apr 2011, 05:06 PM
Maybe I should be asking, is it ok to use the ValueChanged with a maskedInput to do this?  Is it going to override any of the other masking?
0
danparker276
Top achievements
Rank 2
answered on 13 Apr 2011, 06:03 PM
I almost have it, but I can't get the current cursor position.  Or it always changes to 3.  I need to get the cursor postion before the telerik control moves the cursor. SelectionStart isn't the right value when I press '.' ,   it's ok when I press a regular number though.



private void textBoxHours_TextInputStart(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
    if (e.Text.Equals("."))
    {
        int testi = this.textBoxHours.SelectionStart;
    }
}
0
Alex Fidanov
Telerik team
answered on 18 Apr 2011, 08:16 AM
Hi danparker276,

Have you tried setting the SelectionOnFocus property to "Unchanged"? The control handles internally the selection start. You could also try using Dispatcher.BeginInvoke() to change the caret's position in the ValueChanged event.

Regards,
Alex Fidanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
danparker276
Top achievements
Rank 2
answered on 19 Apr 2011, 03:27 AM
SelectionOnFocus doesn't help, like you said it's handeled internally before textinputStart.
The start postion is always 3 when I press a '.' , I have no way of knowing if they pressed it
at starting position 0,1, 2,3...

I'll probably have to take off the mask and do the mask myself.

Should I open up a feature request for it to give you the option to shift the number over if a '.' is pressed? We write applications for accounts that tab through many numeric numbers and are very used to that.
0
Accepted
Alex Fidanov
Telerik team
answered on 22 Apr 2011, 07:11 AM
Hi danparker276,

I apologize for the late response. I think the TextInputStart event would not be appropriate for this. Here is a code snippet that you can use with the KeyDown event:

mask.AddHandler(RadMaskedNumericInput.KeyDownEvent, new KeyEventHandler(mask_KeyDown), true);

private void mask_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Decimal)
    {
        RadMaskedNumericInput numericInput = sender as RadMaskedNumericInput;
        double? value = numericInput.Value;
        int selStart = numericInput.SelectionStart;
        if (value.HasValue)
        {
            string currencySeparator = numericInput.Culture.NumberFormat.CurrencyDecimalSeparator;
            string newValueStr = value.Value.ToString().Replace(currencySeparator, string.Empty).Insert(selStart, currencySeparator);
            double newValue = 0;
            if(double.TryParse(newValueStr, System.Globalization.NumberStyles.Any, numericInput.Culture, out newValue))
            {
                numericInput.Value = Math.Round(newValue, 2);
            }
        }
    }
}
Please note that this code checks only for the Decimal key on the numeric keypad. You would have to add checks for the Oem keys (comma and point).

Kind regards,
Alex Fidanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
danparker276
Top achievements
Rank 2
answered on 22 Apr 2011, 05:20 PM
Thanks works great, brilliant!!! 
Tags
MaskedInput (Numeric, DateTime, Text, Currency)
Asked by
danparker276
Top achievements
Rank 2
Answers by
danparker276
Top achievements
Rank 2
Alex Fidanov
Telerik team
Share this question
or