NumericUpDown:接受逗号和点作为小数点分隔符 [英] NumericUpDown: accept both comma and dot as decimal separator
问题描述
有一种方法可以强制c#NumericUpDown接受逗号和点号,以分隔十进制值?
There's a way to force c# NumericUpDown to accept both comma and dot, to separate decimal values?
我已经自定义了一个文本框来执行此操作(实际上用逗号替换点),但是令我惊讶的是没有其他方法了.
I've customized a textbox to do it (practically replacing dots with commas), but I'm surprised that there isn't another way..
> 此 问题说明了如何更改分隔符,但我想同时使用两者!
This question explains how to change the separator, but I would like to use both!
推荐答案
NumericUpDown控件使用操作系统的区域性来使用逗号或点作为小数点分隔符.
NumericUpDown control uses the culture of the operating system to use comma or dots as a decimal separator.
如果您希望能够处理两个分隔符并将它们视为十进制分隔符(即:不是千位分隔符),则可以使用Validation或手动事件处理,例如:
If you want to be able to handle both separators and consider them as a decimal separator (ie: not a thousand separator), you can use Validation or manual event treatment, for example:
private void numericUpDown1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.Equals('.') || e.KeyChar.Equals(','))
{
e.KeyChar = ((System.Globalization.CultureInfo)System.Globalization.CultureInfo.CurrentCulture).NumberFormat.NumberDecimalSeparator.ToCharArray()[0];
}
}
在此示例中,您将用当前区域性的NumericDecimalSeparator替换每个点和逗号
In this example you will replace every dot and comma by the NumericDecimalSeparator of the current culture
这篇关于NumericUpDown:接受逗号和点作为小数点分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!