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

RadNumericUpDown acepts comma AND dot

2 Answers 152 Views
NumericUpDown
This is a migrated thread and some comments may be shown as answers.
Valentin
Top achievements
Rank 1
Iron
Valentin asked on 20 May 2016, 09:02 AM

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 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 20 May 2016, 12:29 PM
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.
0
Valentin
Top achievements
Rank 1
Iron
answered on 20 May 2016, 12:39 PM

Hello Yana,

 

It is exactly what I searched.

Thank you very much !!

 

Valentin.

Tags
NumericUpDown
Asked by
Valentin
Top achievements
Rank 1
Iron
Answers by
Yana
Telerik team
Valentin
Top achievements
Rank 1
Iron
Share this question
or