我如何检测NumberDecimalSeparator在KeyDown事件(C#) [英] How do I detect a NumberDecimalSeparator in a KeyDown event (C#)

查看:275
本文介绍了我如何检测NumberDecimalSeparator在KeyDown事件(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看到,如果用户按下一个小数点分隔符的文本框,可以允许或禁止它取决于其他参数。

I'm trying to see if the user has pressed a decimal separator in a text box, and either allow or suppress it depending on other parameters.

NumberdecimalSeparator收益为46,或'。'我美国的系统。许多其他国家使用','作为分隔符。当我按下期间KeyDown事件设置键值为190。

The NumberdecimalSeparator returns as 46, or '.' on my US system. Many other countries use ',' as the separator. The KeyDown event sets the KeyValue to 190 when I press the period.

我只是继续寻找逗号/期,或有更好的办法吗?

Do I just continue to look for commas/periods, or is there a better way?

推荐答案

呼叫

CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator

获取当前用户界面区域性的小数点分隔符。您可以使用其他的文化来获取其他语言的分隔符。

gets the decimal separator for the current user interface culture. You can use other cultures to get the separator for other languages.


修改

从该报告在我的系统的166文化( CultureInfo.GetCultures(CultureTypes.SpecificCultures).Count之间的()) ,似乎只有两个分隔符用于:期和逗号。您可以在您的系统尝试这个办法:

From the 166 cultures that are reported in my system (CultureInfo.GetCultures(CultureTypes.SpecificCultures).Count()), it seems that only two separators are used: period and comma. You can try this in your system:

var seps = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
            .Select(ci => ci.NumberFormat.NumberDecimalSeparator)
            .Distinct()
            .ToList();



假设这是真的,这种方法可能会有所帮助(注意,键代码终止或运算与改性剂标志,以消除无效组合):

Assuming that this is true, this method may be helpful (note that the keyCode is OR'ed with the modifiers flag in order to eliminate invalid combinations):

	private bool IsDecimalSeparator(Keys keyCode, Keys modifiers)
	{
		Keys fullKeyCode = keyCode | modifiers;
		if (fullKeyCode.Equals(Keys.Decimal))          // value=110
			return true;

		string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
		if (uiSep.Equals("."))
			return fullKeyCode.Equals(Keys.OemPeriod); // value=190
		else if (uiSep.Equals(","))
			return fullKeyCode.Equals(Keys.Oemcomma);  // value=188
		throw new ApplicationException(string.Format("Unknown separator found {0}", uiSep));
	}



最后一个音符:根据的键枚举,价值46你提到对应于DEL(删除)键(即当Num Lock键为OFF)的地步。

A last note: According to Keys enumeration, the value 46 that you mention corresponds to the DEL (Delete) key (i.e. the point when Num Lock is OFF).

这篇关于我如何检测NumberDecimalSeparator在KeyDown事件(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆