RadNumericUpDown acepts comma AND dot

3 posts, 0 answers
  1. Valentin
    Valentin avatar
    82 posts
    Member since:
    Mar 2016

    Posted 20 May 2016 Link to this post

    Hello,

    I want to know if is it possible to do that RadNumericUpDown accepts dot AND comma ?

    I see this code on the web :

    if (e.KeyChar.Equals('.') || e.KeyChar.Equals(','))
    {
        e.KeyChar = ((System.Globalization.CultureInfo)System.Globalization.CultureInfo.CurrentCulture).NumberFormat.NumberDecimalSeparator.ToCharArray()[0];
    }

    With this code, i did it :
    private void tbOpeSurValeur_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Decimal || e.Key == Key.OemComma)
        {
            e.Key = (Key)((System.Globalization.CultureInfo)System.Globalization.CultureInfo.CurrentCulture).NumberFormat.NumberDecimalSeparator.ToCharArray()[0];
        }
    }

    But, 2 problems :
    - This is not 'e.KeyChar' but 'e.Key'
    - 'e.Key' is in ReadOnly, I can't change his value..

    Can you help me ?
    Thank you.

    Valentin.

  2. Yana
    Admin
    Yana avatar
    4559 posts

    Posted 20 May 2016 Link to this post

    Hi Valentin,

    RadNumericUpDown does not provide a way to set two decimal separators out of the box.

    However, you could use the following approach to achieve the desired - subscribe to the PreviewKeyDown event of the NumericUpDown and in its handler insert the NumberDecimalSeparator of the NumericUpDown control whenever "," or "." are typed in the input field. In other words to insert for example "." in the both cases. Here is some sample code snippet:

    private void numeric_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        var num = sender as RadNumericUpDown;
     
        if (e.Key == Key.Decimal || e.Key == Key.OemComma || e.Key == Key.OemPeriod)
        {
            var textBox = e.OriginalSource as System.Windows.Controls.TextBox;
            if (!textBox.Text.Contains(num.NumberDecimalSeparator))
            {
                textBox.Text += num.NumberDecimalSeparator;
                textBox.CaretIndex = textBox.Text.Length;
            }
            e.Handled = true;
        }
    }

    You will need to set directly set NumberDecimalSeparator property of the NumericUpDown: in this case.

    numeric.NumberDecimalSeparator = ".";


    I hope this helps.

    Regards,
    Yana
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. Valentin
    Valentin avatar
    82 posts
    Member since:
    Mar 2016

    Posted 20 May 2016 in reply to Yana Link to this post

    Hello Yana,

     

    It is exactly what I searched.

    Thank you very much !!

     

    Valentin.

Back to Top