简体   繁体   English

如何在 KeyDown 事件 (C#) 中检测 NumberDecimalSeparator

[英]How do I detect a NumberDecimalSeparator in a KeyDown event (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.我试图查看用户是否在文本框中按下了小数点分隔符,并根据其他参数允许或禁止它。

The NumberdecimalSeparator returns as 46, or '.' NumberdecimalSeparator 返回 46 或 '.' on my US system.在我的美国系统上。 Many other countries use ',' as the separator.许多其他国家/地区使用“,”作为分隔符。 The KeyDown event sets the KeyValue to 190 when I press the period.当我按下句点时,KeyDown 事件将 KeyValue 设置为 190。

Do I just continue to look for commas/periods, or is there a better way?我是继续寻找逗号/句点,还是有更好的方法?

The call电话

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.您可以使用其他文化来获取其他语言的分隔符。


EDIT编辑

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.从我的系统中报告的 166 种文化( CultureInfo.GetCultures(CultureTypes.SpecificCultures).Count() )来看,似乎只使用了两个分隔符:句点和逗号。 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):假设这是真的,此方法可能会有所帮助(请注意, keyCodemodifiers标志进行 OR 运算以消除无效组合):

    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));
    }

A last note: According to Keys enumeration , the value 46 that you mention corresponds to the DEL (Delete) key (ie the point when Num Lock is OFF).最后一点:根据Keys enumeration ,您提到的值 46 对应于 DEL(删除)键(即 Num Lock 关闭时的点)。

The problem here is that the values in the KeyEventArgs are key codes, not characters.这里的问题是KeyEventArgs中的值是键代码,而不是字符。 If you handle KeyPress instead, you will get a char in the KeyPressEventArgs which you can use for the comparison.如果您改为处理KeyPress ,您将在KeyPressEventArgs获得一个字符,您可以将其用于比较。

Note: You should really compare the NumberDecimalSeparator characters as it is a string, not a single character so you need to consider scenarios where there is more than one character in the string.注意:您应该真正比较NumberDecimalSeparator字符,因为它是一个字符串,而不是单个字符,因此您需要考虑字符串中有多个字符的情况。

If you need know if the char pressed is decimal separator:如果您需要知道按下的字符是否是十进制分隔符:

private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
    char separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
    if (e.KeyCahr == separador)
    {
        // true
    }
    else
    {
        // false
    }
}

But, if you need acept decimal numpad key as decimal separator of any culture:但是,如果您需要接受十进制数字键作为任何文化的十进制分隔符:

    private bool decimalSeparator = false;
    private void Control_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Decimal)
            decimalSeparator = true;
    }

    private void Control_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (decimalSeparator)
        {
            e.KeyChar = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
            decimalSeparator = false;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM